struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rick Reumann <strut...@reumann.net>
Subject Re: <html:options> Tag
Date Thu, 27 May 2004 18:54:38 GMT
Brian Boyle wrote:

> I am trying to populate a drop down list with values that I have in a 
> Collection but it is not working. This is what I have so far in my JSP:
> <html:select property="constName">
> <html:options name="constituencies" property="constName" />
> </html:select>
> I have a Collection called constituencies stored in my session and it 
> has javaBeans as elements in it. THese javabeans have a getConstName() 
> attribute so the property="constName" in my tag should be ok.
> However, I think the problem is that my Collection should be a javabean. 
> I read on the struts website 
> http://jakarta.apache.org/struts/userGuide/struts-html.html#options that 
> the javabean represents a collection of other javabeans. How can I have 
> a collection of other javabeans inside my bean?

Documentation on the options tag is horrible and I think it's way too 
confusing. I prefer a JSTL forEach loop and then build the options in 
there. If the value for the option and what you display (the label) are 
going to be different, then yes you need a Collection or array of some 
sort of Object or actually a HashMap of key/value pairs works nicely for 
options. So for example....

Map states = new HashMap();
map.put("FL", "Florida" );
map.put("NJ", "New Jersey");
map.put("NC", "North Carolina");

then to display these options with the value being the abbreviation:

<c:forEach var="mapItem" items="${states}">
     <option value="<c:out value='${mapItem.key}'/>">
	<c:out value='${mapItem.value}'/>
     </option>
</c:forEach>

or if you use a collectoin of beans..

<c:forEach var="bean" items="${stateBeans}">
     <option value="<c:out value='${bean.stateAbbreviation}'/>">
	<c:out value='${bean.stateFullName}'/>
     </option>
</c:forEach>

obviously you'd have set/get methods in your bean for stateAbbreviation, 
stateFullName

-- 
Rick

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


Mime
View raw message