Sort html:select box by localized key in Struts

Discussion in 'Java' started by msa, Dec 15, 2003.

  1. msa

    msa Guest

    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
     
    msa, Dec 15, 2003
    #1
    1. Advertising

  2. msa

    Sudsy Guest

    msa wrote:
    <code snipped>
    > 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>
    </html:select>

    > 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?
     
    Sudsy, Dec 15, 2003
    #2
    1. Advertising

  3. msa

    msa Guest

    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

    Sudsy <> wrote in message news:<>...
    > msa wrote:
    > <code snipped>
    > > 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>
    > </html:select>
    >
    > > 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?
     
    msa, Dec 17, 2003
    #3
  4. msa

    Sudsy Guest

    msa wrote:
    > 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.
     
    Sudsy, Dec 17, 2003
    #4
  5. msa

    msa Guest

    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

    Sudsy <> wrote in message news:<>...
    > 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.
     
    msa, Dec 18, 2003
    #5
  6. msa

    Sudsy Guest

    msa wrote:
    > 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!
     
    Sudsy, Dec 18, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anders Steinlein

    Struts and localized HTML options

    Anders Steinlein, Jul 13, 2003, in forum: Java
    Replies:
    3
    Views:
    2,914
    Anders Steinlein
    Jul 13, 2003
  2. Navin
    Replies:
    1
    Views:
    725
    Ken Schaefer
    Sep 9, 2003
  3. palmiere
    Replies:
    1
    Views:
    431
    Erwin Moller
    Feb 9, 2004
  4. rvelosoo
    Replies:
    1
    Views:
    361
    Bart Van der Donck
    Aug 13, 2008
  5. Andy Dingley

    Localized character sort?

    Andy Dingley, Oct 22, 2009, in forum: Javascript
    Replies:
    10
    Views:
    285
Loading...

Share This Page