You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Andreas Wikberger <an...@blink.se> on 2001/03/07 14:08:25 UTC

String array

Hello,

I have a problem with passing a String array as a parameter to a
predefined java method from my velocity template.

I have a java class in my Context called $util. In this class I have a
method as follows:

public void getList(int noOfRows, String [] attrs){
  ...
}

Re: String array

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Christoph Reck wrote:
> 
> One ugly Java caveat is that a Set or an Object[] cannot be
> easily converted to an String[], event thou they contain
> purely Strings.

One person's "ugly Java caveat"
is another person's "typesafety" :)

(good additional suggestions, though )

Off to convert another to the ways of Velocity :)

geir

-- 
Geir Magnusson Jr.                               geirm@optonline.com
Developing for the web?  See http://jakarta.apache.org/velocity/

Re: String array

Posted by Christoph Reck <Ch...@dlr.de>.
One ugly Java caveat is that a Set or an Object[] cannot be
easily converted to an String[], event thou they contain 
purely Strings.

So if you change your method signature either:
A) getList(int, Object[])
   and use it $util.getList( $rows, $attrs.toArray() )
B) getList(int, Set) or getList(int, ArrayList)
   and use it in a straightforward manner $util.getList($rows, $attrs)

:) Chirstoph Reck

"Geir Magnusson Jr." wrote:
> 
> Andreas Wikberger wrote:
> >
> > Hello,
> >
> > I have a problem with passing a String array as a parameter to a
> > predefined java method from my velocity template.
> >
> > I have a java class in my Context called $util. In this class I have a
> > method as follows:
> >
> > public void getList(int noOfRows, String [] attrs){
> >   ...
> > }
> >
> > >From my velocity template I call this method like this:
> >
> > ...
> > #set( $rows = 5)
> > #set( $attrs = ["name","phone"])
> > $util.getList($rows, $attrs)
> > ...
> 
> $attrs is an ArrayList, not a String[].
> 
> The array and range operators :
> 
> ["a","b"]  or [1..10]
> 
> both return a java.util.ArrayList
>[snip]
> Just change your method, or add one as a wrapper that takes an array
> list and calls the orignal one, and all should be fine.

Re: String array

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Andreas Wikberger wrote:
> 
> Hello,
> 
> I have a problem with passing a String array as a parameter to a
> predefined java method from my velocity template.
> 
> I have a java class in my Context called $util. In this class I have a
> method as follows:
> 
> public void getList(int noOfRows, String [] attrs){
>   ...
> }
> 
> >From my velocity template I call this method like this:
> 
> ...
> #set( $rows = 5)
> #set( $attrs = ["name","phone"])
> $util.getList($rows, $attrs)
> ...

$attrs is an ArrayList, not a String[].

The array and range operators :

["a","b"]  or [1..10]

both return a java.util.ArrayList


> The output of the lines above becomes $util.getList($rows, $attrs) in
> plain text. And in my velocity.log file I get a warning about a
> ReferenceException ( $util.getList($row, $attrs) is not a valid
> reference.)

Because it can't find a method with the right signature.
 
> If I rewove the array as a parameter and only have an integer as
> parameter the method is called and produces the output it is supposed to
> do. Does not  #set( $attrs = ["name","phone"]) a string array equivalent
> to a java String[].

Just change your method, or add one as a wrapper that takes an array
list and calls the orignal one, and all should be fine.


geir

-- 
Geir Magnusson Jr.                               geirm@optonline.com
Developing for the web?  See http://jakarta.apache.org/velocity/