Struts and localized HTML options

A

Anders Steinlein

I have a JSP page where all regular messages are retrieved from property
files using <bean:message ...>. But how do I display a bunch of localized
HTML options in a form? For instance, consider this code snippet:

<html:select property="country" size="1">
<html:eek:ptions property="countries" />
</html:select>

In this example, the options will be populated from the countries field
in my ActionForm, but then I have to hard-code the countries, which is
not what I want.

Do I have to make an Action that populates the relevant fields in the
ActionForm before the view is pulled up, or is there a more "elegant"
approach? If not, how do I go about doing that (getting the correct
properties by code)?

Thanks in advance for any help!

\Anders
 
M

music

Can't remember the exact syntax for this (as it has been over a year since I
have done struts) but you can have the options bind to a list (that you can
load up when the server starts up) that is Application Scope or you can have
the options point to a vector(I believe) which has been preloaded.
 
M

music

Ok. Now my memory is coming back:
http://jakarta.apache.org/struts/userGuide/struts-html.html#options

Use the collection attribute like so:

<html:select property="client" size="1">
<html:eek:ptions collection="searchcriterianumeric" property="value"
labelProperty="label"/>
</html:select>

You can have an action (upon form load) that preloads by building the
collection like so:
ArrayList alNumeric = ....; // Fill the array list...

request.getSession().setAttribute("searchcriterianumeric", alNumeric);

Of course there may be a better way to do this but you should never have to
hardcode any of your options...
 
A

Anders Steinlein

Ok. Now my memory is coming back:
http://jakarta.apache.org/struts/userGuide/struts-html.html#options

Use the collection attribute like so:

<html:select property="client" size="1">
<html:eek:ptions collection="searchcriterianumeric" property="value"
labelProperty="label"/>
</html:select>

You can have an action (upon form load) that preloads by building the
collection like so:
ArrayList alNumeric = ....; // Fill the array list...

request.getSession().setAttribute("searchcriterianumeric", alNumeric);

That's what I was thinking. However, how do I load the correct properties
from code? Is there some functianality in Struts to do this, or do I have
to use PropertyResourceBundle directly? The latter seems a bit silly,
since I will have to know the name (and location?) of the property file
in code (they are defined in struts-config.xml)...

Thanks again!

\Anders
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top