You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by ddd ddd <st...@rediffmail.com> on 2004/03/19 18:55:43 UTC

Pl. help options collection

Dear All 
Can any body complete this oode , I mean to say that what needs to be done in configuration file struts-config, where ARRAY information must be kept.. Can any body complete the code that it will be directly a working code .I have  successfully completed select using simple options .

<html:select  name="StudentForm" property="passwd" >
 <html:options name="StudentForm" property="languages" />
</html:select>

But want to learn using collections I am suing struts1.1.
I am trying from so many days ...pl. help 
Many many thanks in advance pl. help..

Kind Regards 
Strutsguy 
If nay boy needs all code for  above I can send all with java class and config file .

<html:options>
ARRAY information 
ArrayList prod = new ArrayList();
prod.add( new org.apache.struts.util.LabelValueBean( "Widget", "20" ) );
prod.add( new org.apache.struts.util.LabelValueBean( "Sprocket", "29-2a" ) );
prod.add( new org.apache.struts.util.LabelValueBean( "Cog", "29s" ) );
prod.add( new org.apache.struts.util.LabelValueBean( "Dunsel", "943" ) );

LabelValueBean takes the first String argument as the displayed name and the second as the value returned. We’ll see more about this in a bit.
Now that you have prod set up, you pass that as a session or request bean to the JSP.
request.setAttribute( Constants.PRODUCT_KEY, prod );
(the Constants.PRODUCT_KEY is set to our bean name: "products"; we use this to centralize bean name management)
To use the data in the JSP, add the following code:
  <html:select property="product_id" styleClass="prodselect" >
    <html:options collection="products" property="value" labelProperty="label" />
  </html:select>
Notes: product_id is the variable the JSP will set to the product ID value of the product selected by the user. products is the name of the bean (it must match what is in Contants.PRODUCT_KEY -- we probably could've said
... collection="<%= Constants.PRODUCT_KEY %>" ...
in place of hard-coding the name. The choice is up to you.
property="value" and labelProperty="label" are automatically set by org.apache.struts.util.LabelValueBean, so only use the values shown here.
The resulting HTML will look something like:
<select name="product_id" size="1">
<option value="20">Widget</option>
<option value="29-2a">Sprocket</option>
<option value="29s">Cog</option>
<option value="943">Dunsel</option>
</select>