You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by scadh <sc...@hotmail.com> on 2009/03/10 22:36:43 UTC

Trouble accessing Map values in Velocity

I have a Controller method that returns a ModelAndView. The pertinent part of
the code is:

{
Map model = new HashMap();
model.put("pageValues", contentService.getPageValues(channel,
landingName).getValues());

return new ModelAndView("nominate/landing", model);
}

The getValues() method returns a Map, which I put into the model with key
"pageValues".

I am unable to directly reference any values in the Map in the view. I
expected this to work, but it doesn't:

$pageValues.get("key")

However, when referenced indirectly from within a foreach loop, it works
fine:

#foreach($key in $pageValues.keySet())
    <li>$key > $pageValues.get($key)</li>
#end

Is my syntax incorrect attempting to access a Map inside another Map? Can't
seem to figure it out, and time is short! I would greatly appreciate any
assistance that anyone could provide.
-- 
View this message in context: http://www.nabble.com/Trouble-accessing-Map-values-in-Velocity-tp22444107p22444107.html
Sent from the Velocity - User mailing list archive at Nabble.com.


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


Re: Trouble accessing Map values in Velocity

Posted by Nathan Bubna <nb...@gmail.com>.
If it's a HashMap, then you should be fine.  both the class and its
get method are public.  It may be an issue with your key then, as jian
chen suggested.

On Tue, Mar 10, 2009 at 4:45 PM, scadh <sc...@hotmail.com> wrote:
>
> Ah, then perhaps that is my issue -- as there is no class that implements it
> at all. I simply put a HashMap into the model and was attempting to call its
> get() method directly. Is that not allowed? If not, should I write my own
> class that implements the Map interface and put that into the model instead?
>
> Thank you very much -- both of you -- for your fast and courteous replies!
>
>
>
>
> Nathan Bubna wrote:
>>
>> It looks to me like the class that implements the
>> $pageValues.get("key") method is not declared public.  Make sure that
>> both the pageValues class and the get method therein are both declared
>> public.
>>
>> On Tue, Mar 10, 2009 at 2:36 PM, scadh <sc...@hotmail.com> wrote:
>>>
>>> I have a Controller method that returns a ModelAndView. The pertinent
>>> part of
>>> the code is:
>>>
>>> {
>>> Map model = new HashMap();
>>> model.put("pageValues", contentService.getPageValues(channel,
>>> landingName).getValues());
>>>
>>> return new ModelAndView("nominate/landing", model);
>>> }
>>>
>>> The getValues() method returns a Map, which I put into the model with key
>>> "pageValues".
>>>
>>> I am unable to directly reference any values in the Map in the view. I
>>> expected this to work, but it doesn't:
>>>
>>> $pageValues.get("key")
>>>
>>> However, when referenced indirectly from within a foreach loop, it works
>>> fine:
>>>
>>> #foreach($key in $pageValues.keySet())
>>>    <li>$key > $pageValues.get($key)</li>
>>> #end
>>>
>>> Is my syntax incorrect attempting to access a Map inside another Map?
>>> Can't
>>> seem to figure it out, and time is short! I would greatly appreciate any
>>> assistance that anyone could provide.
>>> --
>>> View this message in context:
>>> http://www.nabble.com/Trouble-accessing-Map-values-in-Velocity-tp22444107p22444107.html
>>> Sent from the Velocity - User mailing list archive at Nabble.com.
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>>> For additional commands, e-mail: user-help@velocity.apache.org
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>>
>>
>>
>
> --
> View this message in context: http://www.nabble.com/Trouble-accessing-Map-values-in-Velocity-tp22444107p22446063.html
> Sent from the Velocity - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

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


Re: Trouble accessing Map values in Velocity

Posted by scadh <sc...@hotmail.com>.
Ah, then perhaps that is my issue -- as there is no class that implements it
at all. I simply put a HashMap into the model and was attempting to call its
get() method directly. Is that not allowed? If not, should I write my own
class that implements the Map interface and put that into the model instead?

Thank you very much -- both of you -- for your fast and courteous replies! 




Nathan Bubna wrote:
> 
> It looks to me like the class that implements the
> $pageValues.get("key") method is not declared public.  Make sure that
> both the pageValues class and the get method therein are both declared
> public.
> 
> On Tue, Mar 10, 2009 at 2:36 PM, scadh <sc...@hotmail.com> wrote:
>>
>> I have a Controller method that returns a ModelAndView. The pertinent
>> part of
>> the code is:
>>
>> {
>> Map model = new HashMap();
>> model.put("pageValues", contentService.getPageValues(channel,
>> landingName).getValues());
>>
>> return new ModelAndView("nominate/landing", model);
>> }
>>
>> The getValues() method returns a Map, which I put into the model with key
>> "pageValues".
>>
>> I am unable to directly reference any values in the Map in the view. I
>> expected this to work, but it doesn't:
>>
>> $pageValues.get("key")
>>
>> However, when referenced indirectly from within a foreach loop, it works
>> fine:
>>
>> #foreach($key in $pageValues.keySet())
>>    <li>$key > $pageValues.get($key)</li>
>> #end
>>
>> Is my syntax incorrect attempting to access a Map inside another Map?
>> Can't
>> seem to figure it out, and time is short! I would greatly appreciate any
>> assistance that anyone could provide.
>> --
>> View this message in context:
>> http://www.nabble.com/Trouble-accessing-Map-values-in-Velocity-tp22444107p22444107.html
>> Sent from the Velocity - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Trouble-accessing-Map-values-in-Velocity-tp22444107p22446063.html
Sent from the Velocity - User mailing list archive at Nabble.com.


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


Re: Trouble accessing Map values in Velocity

Posted by Nathan Bubna <nb...@gmail.com>.
It looks to me like the class that implements the
$pageValues.get("key") method is not declared public.  Make sure that
both the pageValues class and the get method therein are both declared
public.

On Tue, Mar 10, 2009 at 2:36 PM, scadh <sc...@hotmail.com> wrote:
>
> I have a Controller method that returns a ModelAndView. The pertinent part of
> the code is:
>
> {
> Map model = new HashMap();
> model.put("pageValues", contentService.getPageValues(channel,
> landingName).getValues());
>
> return new ModelAndView("nominate/landing", model);
> }
>
> The getValues() method returns a Map, which I put into the model with key
> "pageValues".
>
> I am unable to directly reference any values in the Map in the view. I
> expected this to work, but it doesn't:
>
> $pageValues.get("key")
>
> However, when referenced indirectly from within a foreach loop, it works
> fine:
>
> #foreach($key in $pageValues.keySet())
>    <li>$key > $pageValues.get($key)</li>
> #end
>
> Is my syntax incorrect attempting to access a Map inside another Map? Can't
> seem to figure it out, and time is short! I would greatly appreciate any
> assistance that anyone could provide.
> --
> View this message in context: http://www.nabble.com/Trouble-accessing-Map-values-in-Velocity-tp22444107p22444107.html
> Sent from the Velocity - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

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


Re: Trouble accessing Map values in Velocity

Posted by jian chen <ch...@gmail.com>.
Could it be that your key is Integer?

When you access it in Velocity, you are accessing it as string for the key?

Jian
http://www.JiansNet.com
Bilingual English/Chinese site for overseas Chinese and English speakers

On Tue, Mar 10, 2009 at 2:36 PM, scadh <sc...@hotmail.com> wrote:

>
> I have a Controller method that returns a ModelAndView. The pertinent part
> of
> the code is:
>
> {
> Map model = new HashMap();
> model.put("pageValues", contentService.getPageValues(channel,
> landingName).getValues());
>
> return new ModelAndView("nominate/landing", model);
> }
>
> The getValues() method returns a Map, which I put into the model with key
> "pageValues".
>
> I am unable to directly reference any values in the Map in the view. I
> expected this to work, but it doesn't:
>
> $pageValues.get("key")
>
> However, when referenced indirectly from within a foreach loop, it works
> fine:
>
> #foreach($key in $pageValues.keySet())
>    <li>$key > $pageValues.get($key)</li>
> #end
>
> Is my syntax incorrect attempting to access a Map inside another Map? Can't
> seem to figure it out, and time is short! I would greatly appreciate any
> assistance that anyone could provide.
> --
> View this message in context:
> http://www.nabble.com/Trouble-accessing-Map-values-in-Velocity-tp22444107p22444107.html
> Sent from the Velocity - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>