You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Chris Pratt <th...@gmail.com> on 2008/03/04 02:02:21 UTC
Selecting over Static Maps from Resource Bundle
Is it possible to place the data for the list attribute of an
<s:select> tag in a resource bundle (so that it can be
internationalized)? I have tried adding the data to my
application.properties file:
gender.map='FEMALE':'Female','MALE':'Male'
Then referencing it in the JSP as:
<s:select name="gender" id="gender" list="#{(getText('gender.map'))}"/>
But this produces, the fairly unusable code:
<select name="gender" id="gender">
<option value="FEMALE:Female,MALE:Male"></option>
</select>
Is there any way to get it to generate:
<select name="gender" id="gender">
<option value="FEMALE">Female</option>
<option value="MALE">Male</option>
</select>
Thanks.
(*Chris*)
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Selecting over Static Maps from Resource Bundle
Posted by Chris Pratt <th...@gmail.com>.
On Mon, Mar 3, 2008 at 5:37 PM, Martin Gainty <mg...@hotmail.com> wrote:
> construct a public Map in your Action class and insert the key,value
> pairings e.g.
> public Map getDefaultFavouriteCartoonCharacters() {
> Map m = new LinkedHashMap();
> m.put("heMan", "He-Man");
> m.put("popeye", "Popeye");
> m.put("mockeyMouse", "Mickey Mouse");
> return m;
> }
>
> and reference the public Map via list attribute in your jsp
> <s:select
> list="defaultFavouriteCartoonCharacters"
> ...
> />
>
> or just use a set of hardcoded values in key:value pairings for the list
> attribute
> <s:select
> list="#{'heMan':'He-Man', 'popeye':'Popeye', [...]}"
> ..
> />
>
> Martin
>
We could define them on the actions, but since we use lists like the
list of States and Provinces, Gender List and a bunch of others all
over the place, I didn't want to have to define them everywhere.
So we created a BaseAction that defines all those lists, but now that
list is growing and theres a bunch of useless methods on every action.
I was looking for something more dynamic, and I was hoping the
<sarcasm>great and powerful OGNL</sarcasm> could help out.
I guess I'll try to write a static utility method that I can call with
the @ syntax to return a map of data and see how that flies. Thanks
for the input.
(*Chris*)
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Selecting over Static Maps from Resource Bundle
Posted by Martin Gainty <mg...@hotmail.com>.
construct a public Map in your Action class and insert the key,value
pairings e.g.
public Map getDefaultFavouriteCartoonCharacters() {
Map m = new LinkedHashMap();
m.put("heMan", "He-Man");
m.put("popeye", "Popeye");
m.put("mockeyMouse", "Mickey Mouse");
return m;
}
and reference the public Map via list attribute in your jsp
<s:select
list="defaultFavouriteCartoonCharacters"
...
/>
or just use a set of hardcoded values in key:value pairings for the list
attribute
<s:select
list="#{'heMan':'He-Man', 'popeye':'Popeye', [...]}"
..
/>
Martin
----- Original Message -----
From: "Chris Pratt" <th...@gmail.com>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Monday, March 03, 2008 8:02 PM
Subject: Selecting over Static Maps from Resource Bundle
> Is it possible to place the data for the list attribute of an
> <s:select> tag in a resource bundle (so that it can be
> internationalized)? I have tried adding the data to my
> application.properties file:
>
> gender.map='FEMALE':'Female','MALE':'Male'
>
> Then referencing it in the JSP as:
>
> <s:select name="gender" id="gender" list="#{(getText('gender.map'))}"/>
>
> But this produces, the fairly unusable code:
>
> <select name="gender" id="gender">
> <option value="FEMALE:Female,MALE:Male"></option>
> </select>
>
> Is there any way to get it to generate:
>
> <select name="gender" id="gender">
> <option value="FEMALE">Female</option>
> <option value="MALE">Male</option>
> </select>
>
> Thanks.
> (*Chris*)
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Selecting over Static Maps from Resource Bundle
Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Dave Newton wrote:
> --- Chris Pratt <th...@gmail.com> wrote:
>
>> Is it possible to place the data for the list attribute of an
>> <s:select> tag in a resource bundle (so that it can be
>> internationalized)? I have tried adding the data to my
>> application.properties file:
>>
>> gender.map='FEMALE':'Female','MALE':'Male'
>>
>> Then referencing it in the JSP as:
>>
>> <s:select name="gender" id="gender" list="#{(getText('gender.map'))}"/>
>>
>
> You could try something like #{%{getText...}} but I suspect that wouldn't
> work.
>
> You might also try creating it with a specific map type:
>
> #@java.util.LinkedHashMap@{ getText... }
>
> and see if that affects statement parsing. I suspect that won't work either,
> though.
>
> You could also do the work in the action, converting the text values into a
> real list/map.
>
> Dave
>
>
>
Interesting little problem... so you want to call a method, get the
string result and have OGNL evaluate that string as if it were a Map.
Sounds plausible, but I don't like your chances. I throw a breakpoint
in ListUIBean to see what object you're getting from OGNL.
My guess is based on Dave's second one:
<s:select name="gender" id="gender" list="#@java.util.LinkedHashMap@{(getText('gender.map'))}"/>
parenthesis to ensure getText is evaluated first (should be anyway)
Then construct a map [1] using the string as the parameter
[1]
http://www.ognl.org/2.6.9/Documentation/html/LanguageGuide/collectionConstruction.html#mapConstruction
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Selecting over Static Maps from Resource Bundle
Posted by Dave Newton <ne...@yahoo.com>.
--- Chris Pratt <th...@gmail.com> wrote:
> Is it possible to place the data for the list attribute of an
> <s:select> tag in a resource bundle (so that it can be
> internationalized)? I have tried adding the data to my
> application.properties file:
>
> gender.map='FEMALE':'Female','MALE':'Male'
>
> Then referencing it in the JSP as:
>
> <s:select name="gender" id="gender" list="#{(getText('gender.map'))}"/>
You could try something like #{%{getText...}} but I suspect that wouldn't
work.
You might also try creating it with a specific map type:
#@java.util.LinkedHashMap@{ getText... }
and see if that affects statement parsing. I suspect that won't work either,
though.
You could also do the work in the action, converting the text values into a
real list/map.
Dave
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org