You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by jt...@honda.co.nz on 2004/06/24 05:09:36 UTC

Struts - How to recover collection element





I've loaded a collection of valid options and their descriptions as a
ServletContext attribute, for use in an <html:options> tag.
It works as intended  in a generated html <select> tag.
Once a user selects an option, the option value (rather than the
description) is recorded in a form bean, and the form bean is forwarded to
another form.
So far so good.

What I'd like to do in the next form is display the description of the
selected option, using the option as a lookup key to the original
collection.
Is there an existing struts tag that will allow me to do this?


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


Re: Struts - How to recover collection element

Posted by Bill Siggelkow <bi...@bellsouth.net>.
There are a couple of ways of doing this. I think the best approach is 
to wrap your collection in a JavaBean that is stored in the session. 
Something like the following:

public class OptionsHolder {
   Map options = new HashMap();
   public OptionsHolder() {
     options.put("key1", new LabelValueBean("foo","key1"));
     options.put("key2", new LabelValueBean("foo","key1"));
   }
   public Collection getOptionsCollection() {
     return options.values();
   }
   public Map getOptionsMap() {
     return options;
   }
}

You can then use the getOptionsCollection() method to render the options 
in the first form. Then in the page where you want to display the value 
for the key, you can use JSTL. Assuming the ServletContext attribute 
name were "optionsHolder" and the key was in the property 'key' on the form:

<c:out value="${optionsHolder.optionsMap[MyForm.key]}"/>

Another alternative is to create a mapped property in the OptionsHolder 
bean; then use Struts support for mapped properties in the bean:write tag.

Finally, a third alternative would be to fetch the value in the action 
that processes the form. Extract the desired value then set it as a 
request attribute for display on the subsequent JSP. With this approach 
you might not need to use the OptionsHolder class and instead just 
iterate over the collection looking for a match.


jthompson@honda.co.nz wrote:

> I've loaded a collection of valid options and their descriptions as a
> ServletContext attribute, for use in an <html:options> tag.
> It works as intended  in a generated html <select> tag.
> Once a user selects an option, the option value (rather than the
> description) is recorded in a form bean, and the form bean is forwarded to
> another form.
> So far so good.
> 
> What I'd like to do in the next form is display the description of the
> selected option, using the option as a lookup key to the original
> collection.
> Is there an existing struts tag that will allow me to do this?


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