velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Christian <si...@stoutstick.com>
Subject Re: Velocity alternative for WebMacro's Template.getParam?
Date Mon, 15 Nov 2004 11:25:46 GMT
Hi Stefan,

I don't think there is a direct alternative to the Templage.getParam 
function, however I guess it rather depends what you want to to with the 
parameters i.e. are you intending to validate the input and if so to 
what level, or do you just need access to them?

If you need to do 'proper' validation, a common solution (though it 
obviously has  much more widespread benefits/effects) is to use Velocity 
with Struts and use the latters' form validation which is defined on a 
per-action basis in XML.

Alternatively if you just want to check that the parameters exist you 
could add a header to each template which uses a tool (some POJO) to 
check the parameters and perform some exit strategy it all's not well.

If you need access to the parameters and want to verify their format 
take a look at the ParameterParser from the VelocityTools project:

   http://jakarta.apache.org/velocity/tools/view/ParameterParser.html

You don't need to use VelocityTools with it - just construct an instance 
and drop it into the request, then you can access the parameters like 
$parameters.getString( "foo" ), $parameters.getBoolean ( "bar" ) etc.

Or of course use similar to populate the context with all the available 
parameters and values. the VelocityViewServlet/VelocityLayoutServlet 
will do this for you if that's an option.

hth,

- simon


Stefan Rotman wrote:
> Hi,
> 
> I'm checking out the possibility to migrate some of our WebMacro
> projects to Velocity. Now there's one thing that we use a lot in
> WebMacro, for which I can't find a Velocity alternative just yet.
> 
> We use the Templage.getParam function from WebMacro to get some
> parameters before
> parsing the template.
> 
> Javadoc at:
> http://www.webmacro.org/api/org/webmacro/Template.html#getParam(java.lang.String)
> 
> Let me explain with a little example:
> 
> We have a WebMacro template (let's call it template.wm) looking like this:
> 
> <html>
> <body>
> #param $require = ["foo", "bar"]
> 
> Foo value is $foo
> Bar value is $bar
> </body>
> </html>
> 
> Now when our servlet is instructed to return the template.wm Template,
> the code looks somewhat like this:
> 
> public class FooServlet extends WMServlet {
>     public Template handle(WebContext context) {
>         Template template = getTemplate("template.wm");
>         Object[] requiredParams = (Object[])template.getParam("require");
>         for (int i = 0; i < requiredParams.length; i++)
>             context.put(requiredParams[i], "-Value for "+requiredParams[i]+"-");
>         return template;
>     }
> }
> 
> This way the values for foo and bar are known when the template is displayed.
> 
> Is there a right way to do this in Velocity? The closest I came to
> this using Velocity is creating a new VelocityContext, merge the
> Template with this context, and get the value for require from that
> context, but when doing this I get warnings in my log files about $foo
> and $bar that are not yet set, so I really would like a "cleaner" way
> to do this.
> 
> Thanks in advance,
> Stefan.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Mime
View raw message