You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by mi...@bodaro.com on 2001/02/10 04:17:50 UTC

Why cant I pass "arrays" to object methods?

Consider the following VTL:

    #set ($array=["not", "my"])
    #foreach ($r in $array)
        $r
    #end

this works as expected. 

And if I do 

$object.methodA("string") – this works,	// methodA takes a string

But if I do

$object.methodB($array)	// methodB takes an Object[]

and I have a method named method in the object "represented" by $object – 
and the method takes an Object[] as a parameter it does not work.

I have also tried with a methodB that takes a String[] - still dont work..

BUT if I do
$object.methodB($anotherobject) and the $anotherobject represents an 
actual array (set previously in thecontext of course) then it does work.

Any ideas as to why this doesnt work and how to fix it...I think being 
able to create Object[] arrays using the ["aaa",$raaa] syntax is very 
important. And the doco says that using ["aaa", $bcd] is a "object array" 
in the users guide...

I am not quite yet on the mailing list so I would appreciate replies to 
mike@bodaro.com.

Mike Papper

Re: Why cant I pass "arrays" to object methods?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
mike@bodaro.com wrote:
> 
> Thanks, I tried it and it now works well. Very cool, even does method
> overloading.
> 
> Next question ( a bit harder):
> 
> I would like to parse the web page (*.vm) myself, sending Velocity
> portions of the template page a bit at a time.
> 
> Another way to look at is to send Velocity a portion of the page (a
> snippit of the template) have it process, then my servlet does some other
> stuff, send Velocity more VTL code, do some more stuff,...

Ok.  But out of sheer curiosity, what's the point?

If you are trying to make a 'dynamic' template of sorts, where you
construct the final output from pieces, there are several features of
Velocity and VTL that help in doing this, while maintaining the
convenience of a template that's easy to maintain, edit and extend.

1) #parse( arg ) : will take the arg, parse and render it against the
current context, and insert that stream into the output stream.
2) #include( arg ) : will take the arg and insert as is into the stream.
(no parseing or rendering)
3) Velocimacros : go see the docs :)

This may not be what you want to do though.. I am just guessing.

> Is there a way to stream text to Velocity rather than passing a filename?
> Could I easily? Implment this as an alternative method call in
> VelocityServlet?

Yes, there is a way to be more 'granular' about processing.  There is a
class called Velocity that is currently in the whiteboard/geir
directory, and will be checked into CVS this morning as
org.apache.velocity.util.Velocity.

This class has two static methods 
 
 public static  boolean evaluate( Context context,  Writer out, String
logTag, String instring )
 public static boolean evaluate( Context context, Writer writer, String
logTag, InputStream instream )
 
that allow you to parse and process an InputStream or String into a
Writer, just like a template would be.  I think this is what you are
asking for.

You would have to make your own servlet, but it is really easy.  Just
make sure you init() the Velocity engine correctly - see how
VelocityServlet does it.

As I don't know what you are doing, or what your experience level is, I
will spare you the preaching about MVC/Model2 architecture, and what
Velocity does to help support this design pattern.  But if you are
unfamiliar with this, I would give it a look-see :

http://www.javaworld.com/javaworld/jw-12-1999/jw-12-ssj-jspmvc.html

geir


-- 
Geir Magnusson Jr.                               geirm@optonline.com
Velocity : it's not just a good idea. It should be the law.
http://jakarta.apache.org/velocity

Re: Why cant I pass "arrays" to object methods?

Posted by mi...@bodaro.com.
Thanks, I tried it and it now works well. Very cool, even does method 
overloading. 

Next question ( a bit harder):

I would like to parse the web page (*.vm) myself, sending Velocity 
portions of the template page a bit at a time.

Another way to look at is to send Velocity a portion of the page (a 
snippit of the template) have it process, then my servlet does some other 
stuff, send Velocity more VTL code, do some more stuff,...

Is there a way to stream text to Velocity rather than passing a filename? 
Could I easily? Implment this as an alternative method call in 
VelocityServlet?

Mike Papper

>>>>>>>>>>>>>>>>>> Original Message <<<<<<<<<<<<<<<<<<

On 2/9/2001, 9:24:19 PM, "Geir Magnusson Jr." <ge...@optonline.net> wrote 
regarding Re: Why cant I pass "arrays" to object methods?:


> It's an ArrayList, not an Object[].  Sorry about that.  We'll update the
> documentation.

> geir

> mike@bodaro.com wrote:
> >
> > Consider the following VTL:
> >
> >     #set ($array=["not", "my"])
> >     #foreach ($r in $array)
> >         $r
> >     #end
> >
> > this works as expected.
> >
> > And if I do
> >
> > $object.methodA("string") - this works, // methodA takes a string
> >
> > But if I do
> >
> > $object.methodB($array) // methodB takes an Object[]
> >
> > and I have a method named method in the object "represented" by $object -
> > and the method takes an Object[] as a parameter it does not work.
> >
> > I have also tried with a methodB that takes a String[] - still dont 
work..
> >
> > BUT if I do
> > $object.methodB($anotherobject) and the $anotherobject represents an
> > actual array (set previously in thecontext of course) then it does work.
> >
> > Any ideas as to why this doesnt work and how to fix it...I think being
> > able to create Object[] arrays using the ["aaa",$raaa] syntax is very
> > important. And the doco says that using ["aaa", $bcd] is a "object array"
> > in the users guide...
> >
> > I am not quite yet on the mailing list so I would appreciate replies to
> > mike@bodaro.com.
> >
> > Mike Papper

> --
> Geir Magnusson Jr.                               geirm@optonline.com
> Velocity : it's not just a good idea. It should be the law.
> http://jakarta.apache.org/velocity

Re: Why cant I pass "arrays" to object methods?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
It's an ArrayList, not an Object[].  Sorry about that.  We'll update the
documentation.

geir

mike@bodaro.com wrote:
> 
> Consider the following VTL:
> 
>     #set ($array=["not", "my"])
>     #foreach ($r in $array)
>         $r
>     #end
> 
> this works as expected.
> 
> And if I do
> 
> $object.methodA("string") - this works, // methodA takes a string
> 
> But if I do
> 
> $object.methodB($array) // methodB takes an Object[]
> 
> and I have a method named method in the object "represented" by $object -
> and the method takes an Object[] as a parameter it does not work.
> 
> I have also tried with a methodB that takes a String[] - still dont work..
> 
> BUT if I do
> $object.methodB($anotherobject) and the $anotherobject represents an
> actual array (set previously in thecontext of course) then it does work.
> 
> Any ideas as to why this doesnt work and how to fix it...I think being
> able to create Object[] arrays using the ["aaa",$raaa] syntax is very
> important. And the doco says that using ["aaa", $bcd] is a "object array"
> in the users guide...
> 
> I am not quite yet on the mailing list so I would appreciate replies to
> mike@bodaro.com.
> 
> Mike Papper

-- 
Geir Magnusson Jr.                               geirm@optonline.com
Velocity : it's not just a good idea. It should be the law.
http://jakarta.apache.org/velocity

Re: Why cant I pass "arrays" to object methods?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Christoph Reck wrote:
>
> Geir, I do like the fact that its now an array list, but it will cause
> some problems for objects with exisiting methods that are taking Object[]
> as a parameters. The above workaround is sufficient. but makes
> it hard to do a direct call:
>         #set( $list = $tools.split($aStringRef, [$CRLF, $NL, ',']) )
> A different solution would have been to allow accessing public fields
> directly, e.g.
>         $array.length
> Just my 2c, and not very important.

I know, and that was tossed about a little.  But, we don't allow access
to public fields (thats kinda poor practice, right?) and the richness
and flexibility of ArrayList seems to make up for the extra step of 
    #set( $goo = [$CRLF, $NL, ','] )
    #set( $list = $tools.split($aStringRef, $goo) )

?

geir

-- 
Geir Magnusson Jr.                               geirm@optonline.com
Velocity : it's not just a good idea. It should be the law.
http://jakarta.apache.org/velocity

Re: Why cant I pass "arrays" to object methods?

Posted by Christoph Reck <Ch...@dlr.de>.
> But if I do
> 
> $object.methodB($array) // methodB takes an Object[]
> 
> and I have a method named method in the object "represented" by $object -
> and the method takes an Object[] as a parameter it does not work.

So do:

$object.methodB( $array.toArray() )

Since #set( $array = ["not", "my"] ) due to a recent enhancement
generates an ArrayList.

Geir, I do like the fact that its now an array list, but it will cause
some problems for objects with exisiting methods that are taking Object[]
as a parameters. The above workaround is sufficient. but makes
it hard to do a direct call:
	#set( $list = $tools.split($aStringRef, [$CRLF, $NL, ',']) )
A different solution would have been to allow accessing public fields
directly, e.g.
	$array.length
Just my 2c, and not very important.

:) Christoph