Sort html:select box by localized key in Struts

M

msa

Hi there,

I'm using an <html:select> drop down box to list some items. I
receive a list of values as a collection (itemList) from a custom tag.
I use <html:eek:ption> to display each option because I want to show a
localized version of each value for the user.

Here is my code:
<html:select property="item" >

<logic:iterate id="currentItem" name="itemList">
<bean:define id="itemName" name="currentItem"
type="java.lang.String" />

<% String itemKey = "item." + itemName; %>

<html:eek:ption key="<%= itemKey %>" value="<%= itemName %>" />
</logic:iterate>

</html:select>

Values:
car
fish
house

Localization keys in message resource file:
item.car = Auto
item.fish = Poisson
item.house = Maison

My question is this: How do you recommend I sort my options
alphabetically by the localized key value, instead of the actual
value? Right now my box is in the order "Auto, Poisson, Maison"
whereas I want it "Auto, Maison, Poisson".

All I can think of doing is parsing through the itemList collection
when I retrieve it in my custom tag and while doing so, grabbing the
localization key from the property file and creating a new collection
that I return to the JSP for use in the <html:select> box. But this
requires writing a file parser for the message resource file, which
sounds like more work than this needs to be, and I'd rather keep the
custom tag generic and do the sorting in the JSP.

Does Struts have any tools for getting localized values from the
message resources files through a Java class?

Any advice would be greatly appreciated!

Thanks!

Marnie
 
S

Sudsy

msa wrote:
My question is this: How do you recommend I sort my options
alphabetically by the localized key value, instead of the actual
value? Right now my box is in the order "Auto, Poisson, Maison"
whereas I want it "Auto, Maison, Poisson".
TreeMap.

All I can think of doing is parsing through the itemList collection
when I retrieve it in my custom tag and while doing so, grabbing the
localization key from the property file and creating a new collection
that I return to the JSP for use in the <html:select> box. But this
requires writing a file parser for the message resource file, which
sounds like more work than this needs to be, and I'd rather keep the
custom tag generic and do the sorting in the JSP.

So why not do it in the Action? Here's a sample execute method:

public ActionForward execute( ActionMapping mapping,
ActionForm actionForm, HttpServletRequest req,
HttpServletResponse resp ) throws Exception {
HttpSession sess = req.getSession();

// dummy data

Vector v = new Vector();
v.add( "fish" );
v.add( "house" );
v.add( "car" );

// this is where we build the TreeMap

TreeMap result = new TreeMap();
Locale locale = getLocale( req );
MessageResources msgs = getResources( req );
String key = null;
String value = null;
for( int i = 0; i < v.size(); i++ ) {
value = "item." + (String) v.elementAt( i );
key = msgs.getMessage( locale, value );
result.put( key, value );
}
sess.setAttribute( "itemList", result );
return( mapping.findForward( "continue" ) );
}

And here's the JSP code:

<html:select property="item" >
<logic:iterate id="currentItem" name="itemList">
<bean:define id="itemValue" name="currentItem"
property="value" type="java.lang.String"/>
<html:eek:ption value="<%= itemValue %>"><bean:write
name="currentItem" property="key"/></html:eek:ption>
</logic:iterate>
Does Struts have any tools for getting localized values from the
message resources files through a Java class?

Any advice would be greatly appreciated!

Thanks!

Marnie

Does this help?
 
M

msa

Thanks for your reply, Sudsy!

This is looking very close to what I want to do. The trouble is, I'll
need to do this in a custom tag. I do have an initializing action for
the page, but I was finding that my drop down list values were getting
lost if control was returned to the page after an error occurred. So,
I get all of my page lists with custom tags.

Are there getLocale() and getResources() methods that I can use in a
custom tag? I think I can translate the rest into tag-friendly code.

Thanks again!
Marnie
 
S

Sudsy

msa said:
This is looking very close to what I want to do. The trouble is, I'll
need to do this in a custom tag. I do have an initializing action for
the page, but I was finding that my drop down list values were getting
lost if control was returned to the page after an error occurred. So,
I get all of my page lists with custom tags.

Are there getLocale() and getResources() methods that I can use in a
custom tag? I think I can translate the rest into tag-friendly code.

Thanks again!
Marnie

The problem is that MessageResources is a Struts class while custom
tags merely require the services of a servlet container. So while it
might be possible to build something with bailing wire and some duct
tape, trying to tie your tags to Struts is a "very bad idea"!
You mention that your values were lost if an error occured during
page processing. Perhaps the better approach would be to ensure that
the setting of the attribute in the appropriate context is performed
before any error situation could be encountered.
If you're using Struts for validation then your values shouldn't be
lost if you have to return to the page specified by the input
attribute of the action tag.
Of course, without seeing the code or the actual operation of your
application I'm just taking shots in the dark here.
 
M

msa

Hi again,

Oh, I didn't realise that using custom tags with Struts wasn't a good
idea. I think I can add the collections that I use in the page to the
request object again if an error occurs, instead of getting them from
a tag in the JSP. I'll try that. I don't store any of my collections
in session scope because of the performance hit that will take (my app
has to support upward of 10000 users), so that's why the values are
lost.

On returning to the page from an error, all of the ActionForm values
are preserved. Is it better if I make an ActionForm property for each
collection instead of placing them in the request object? I've never
done anything that fancy before, using only java.lang.String types for
my properties. But that's possible, right? Can the ActionForm
properties be of custom types, like <form-property name="itemList"
type="com.myCompany.myClass"/>? Forgive me if that's a silly
question.

Thanks again, Sudsy! I'm really learning some good stuff here!

-Marnie
 
S

Sudsy

msa said:
Hi again,

Oh, I didn't realise that using custom tags with Struts wasn't a good
idea. I think I can add the collections that I use in the page to the
request object again if an error occurs, instead of getting them from
a tag in the JSP. I'll try that. I don't store any of my collections
in session scope because of the performance hit that will take (my app
has to support upward of 10000 users), so that's why the values are
lost.

I didn't mean to suggest that using custom tags with Struts was a bad
idea. Heck, I use tiles, logic, bean and html tags all the time!
What's wrong is introducing dependencies, i.e. when your custom tags
rely on the Struts framework being available. Custom tags should be
portable to any servlet container.
Hint: You could actually store your MessageResources in application
scope if you were so inclined, although I think that's a really bad
plan!
On returning to the page from an error, all of the ActionForm values
are preserved. Is it better if I make an ActionForm property for each
collection instead of placing them in the request object? I've never
done anything that fancy before, using only java.lang.String types for
my properties. But that's possible, right? Can the ActionForm
properties be of custom types, like <form-property name="itemList"
type="com.myCompany.myClass"/>? Forgive me if that's a silly
question.

Not a silly question at all! And both possible and practical. Best
I can suggest is that you visit my page at www.sudsy.net, click on
the "Technology" button and read the Struts pages. You'll find
these very issues addressed in the "Advanced Struts" article.
Thanks again, Sudsy! I'm really learning some good stuff here!

-Marnie

That's what we like to hear!
 

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

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top