You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by James Cook <ji...@iname.com> on 2001/11/27 15:59:19 UTC

Indexed getters

Is it possible to access a get method that takes a parameter? I have tried
more options than I care to mention without success.

	public int getNumber(int number) {
		return number;
	}

	$number(5)
	$getNumber(5)
	${getNumber(5)}

Of course, if I create a getNumber() method without a parameter, I can
access a return value by using:

	$number or $getNumber

What is interesting is that the VTL spec states that I can use:

	${getNumber()}
	$getNumber()

But this returns:
	5()}
	5()

Which seems to indicate a problem parsing the parameter section of a
statement.

-jim


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Indexed getters

Posted by Simon Christian <si...@cpd.co.uk>.
Remember you need to reference the method on the object, so you shuold
be using something like:

$foo.getNumber(5)

- simon


James Cook wrote:
> 
> Is it possible to access a get method that takes a parameter? I have tried
> more options than I care to mention without success.
> 
>         public int getNumber(int number) {
>                 return number;
>         }
> 
>         $number(5)
>         $getNumber(5)
>         ${getNumber(5)}
> 
> Of course, if I create a getNumber() method without a parameter, I can
> access a return value by using:
> 
>         $number or $getNumber
> 
> What is interesting is that the VTL spec states that I can use:
> 
>         ${getNumber()}
>         $getNumber()
> 
> But this returns:
>         5()}
>         5()
> 
> Which seems to indicate a problem parsing the parameter section of a
> statement.
> 
> -jim
>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Indexed getters

Posted by Rickard Öberg <ri...@xpedio.com>.
Geir Magnusson Jr. wrote:

>>>-----Original Message-----
>>>From: Stephane MOR [mailto:stephanemor@yahoo.fr]
>>>Sent: Tuesday, November 27, 2001 10:22 AM
>>>
>><snip>
>>
>>>Then, in the template, you can use :
>>>
>>>$foo.getBar("blahblah")
>>>
>>>to get the returned String.
>>>
>>>
>>That is how I understand it as well, but it doesn't work that way for me. I
>>am relying on webwork to provide the "context". It has a servlet that
>>extends VelocityServlet that provides the WebWork context object
>>(ValueStack) to the VelocityEngine. Perhaps there is a problem with this
>>process.
>>
>>
> 
> Hm.  I would bet that WebWork works :) do you put the object into the
> context, or does webwork do it for you?


WebWork puts the action into the context, so that all properties of the action can be accessed in the page.


Thus, if you use $foo it will be evaluated in WebWork to 
<theaction>.getFoo().

However, if you use $foo.bar this will be evaluated to 
<theaction>.getFoo() in WebWork, but getBar() will be done by Velocity. 
WebWork is only in charge of the first part of the value resolution.

/Rickard

-- 
Rickard Öberg


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Indexed getters

Posted by Rickard Öberg <ri...@xpedio.com>.
James Cook wrote:

> It appears that the ValueStack is more restrictive on the methods it exposes
> than a typical Velocity context.

I don't think so. If you feed in "getNumber(5)" it should get back the 
same thing as you'd expect from Velocity. However, the question is: are 
method lookups processed against the context at all? I would guess not. 
AFAICT the first part after $ is always a context lookup name. This is 
why your output is (5), since Velocity will parse to getNumber, and then 
it will stop, since there is no . to access the next part (which could 
be "getFoo(12)" or similar).

It might be possible to get it to work if you can somehow access the top 
of the stack of WebWork with the first name. "." does that, i.e. 
"$..getNumber(5)" would work (since . would resolve to your action), 
although I'm not sure that is valid Velocity syntax :-)

/Rickard

-- 
Rickard Öberg


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Indexed getters

Posted by James Cook <ji...@iname.com>.
> -----Original Message-----
> From: Geir Magnusson Jr. [mailto:geirm@optonline.net]

> Hm.  I would bet that WebWork works :) do you put the object into the
> context, or does webwork do it for you?

WebWork does it. It has the concept of a ValueStack which is the "normal"
context exposed by WebWork. This ValueStack is wrapped by a class:

   static class WebWorkVelocityContext extends VelocityContext {
      ValueStack stack;

      WebWorkVelocityContext(ValueStack aStack) {
         stack = aStack;
      }

      public boolean internalContainsKey(java.lang.Object key) {
         boolean contains = super.internalContainsKey(key);
         return contains ? true : stack.test(key.toString());
      }

      public Object internalGet(String key) {
         return super.internalContainsKey(key) ?
                super.internalGet(key) :
                stack.findValue(key);
      }
   }

It appears that the ValueStack is more restrictive on the methods it exposes
than a typical Velocity context.

-jim


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Indexed getters

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 11/27/01 10:42 AM, "James Cook" <ji...@iname.com> wrote:

>> -----Original Message-----
>> From: Stephane MOR [mailto:stephanemor@yahoo.fr]
>> Sent: Tuesday, November 27, 2001 10:22 AM
> 
> <snip>
> 
>> Then, in the template, you can use :
>> 
>> $foo.getBar("blahblah")
>> 
>> to get the returned String.
>> 
> 
> That is how I understand it as well, but it doesn't work that way for me. I
> am relying on webwork to provide the "context". It has a servlet that
> extends VelocityServlet that provides the WebWork context object
> (ValueStack) to the VelocityEngine. Perhaps there is a problem with this
> process.
> 

Hm.  I would bet that WebWork works :) do you put the object into the
context, or does webwork do it for you?

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety." - Benjamin Franklin



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Indexed getters

Posted by James Cook <ji...@iname.com>.
> -----Original Message-----
> From: Stephane MOR [mailto:stephanemor@yahoo.fr]
> Sent: Tuesday, November 27, 2001 10:22 AM

<snip>

> Then, in the template, you can use :
>
> $foo.getBar("blahblah")
>
> to get the returned String.
>

That is how I understand it as well, but it doesn't work that way for me. I
am relying on webwork to provide the "context". It has a servlet that
extends VelocityServlet that provides the WebWork context object
(ValueStack) to the VelocityEngine. Perhaps there is a problem with this
process.

-jim


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Indexed getters

Posted by Stephane MOR <st...@yahoo.fr>.
Hi Jim,

>From: "James Cook" <ji...@iname.com>


> Is it possible to access a get method that takes a parameter? I have tried
> more options than I care to mention without success.

Let's say you have a class named "Foo", which contains a method "public
String getBar(String thisIsAString)".

Your class contains a Context named "context".

You can add an instance of the class to the context, using :

context.put("foo", new Foo())

Then, in the template, you can use :

$foo.getBar("blahblah")

to get the returned String.

If you had a method named getBar() (without parameters), you could use :

$foo.Bar          // for each method named "getXXX()" without parameters,
you
                         // can use the short form $foo.XXX

Here you are !
It works for me, I'll hope that it'll work for you !

> -jim
Stephane


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>