Re: Declare String array and pass as method arg

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Thursday, August 20, 2009, 5:26:45 PM, strutstwouser wrote:
>
> Hi,
> I have a static method that has a vararg argument. This has to be invoked
> from an FTL passing it a String array. How do i declare this String array in
> FTL and invoke the method with it.

Since FTL and Java has separate type-systems, if you create a value in
FTL, it must be converted to the Java type when a Java method is
called. For primitive values automatic conversion works, but not for
objects. So if you have something like ["foo", "bar"], it won't be
automatically converted to things like String[], ArrayList, etc. Which
is a BAD THING, but... well, this is where we pay for the flexibility
this design elsewhere gives (I'm not saying it worked out very
well...). But, if you have a Java method that returns a String[], or
you have a String[] in the data-model (the point is that it wasn't
created by FTL itself), then even after it was converted (wrapped) to
an FTL value, it can be converted back (unwrapped) to what it was. So
a workaround would be to do something like
someObject.someMethod(util.newStringArray("foo", "bar")), where
util.stringArray is a Java method with vararg parameter list, that
returns a String[]. Yeah, ugly...

As of static method invocation, you can only touch in a template that
was explicitly exposed in the data-model. So for example the Java
classes are not available by default. Static methods can be easily
exposed as seen on
http://freemarker.org/docs/pgui_misc_beanwrapper.html#autoid_54, but
they have to be exposed by whoever build the data-model. If the CMS
doesn't do that, then in principle it was the decision of the CMS
authors to not allow arbitrary static method calls for the templates.
But, there is an exploit to work that around: ?new (see
http://freemarker.org/docs/ref_builtins_expert.html#ref_builtin_new).
Combine that with a TemplateMethodModelEx that you implement, and you
can do whatever you want... like you could gets the statics of a class
specified by its name.

> The static method invocation can be invoked in Struts2 as
> <@s.property value="@classname@methodname(args)"/> but how do i pass
> args such that it maintains the array nature and Struts2 does not
> complain too.. The limitation is that the method invocation can be
> done only from the FTL since the page is driven by a CMS engine and
> there's no intermediate "handler" provided where code can be
> executed.
>
> Thanks!

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.