The Selection List - Single Type
Since the single type selection list returns one value only, we
can use the same setup in struts-config as for the radio
buttons:
<form-bean name="singleSelectForm"
type="org.apache.struts.action.DynaActionForm">
<form-property name="control" type="java.lang.String"/>
</form-bean>
Now assume that we have a bean, Customer, with String properties
firstName,
lastName, address, and id. If you have a
Collection of Customer beans then you can show a list of
all the customers using jsp-code like this:
<html:select property="control" size="2">
<logic:iterate id="customer" name="customers" type="hansen.playground.Customer">
<html:option value="<%=customer.getId()%>">
<bean:write name="customer" property="firstName"/>
<bean:write name="customer" property="lastName"/>
</html:option>
</logic:iterate>
</html:select>
You'll have to specify the type attribute of the
iterate tag or the scriptlet will fail.
There is a much nicer solution, however:
<html:select property="control" size="2">
<html:options collection="customers" property="id" labelProperty="fullName"/>
</html:select>
This time we use the options
tag, where the collection attribute gives the name
of the Collection of beans, and
property gives the name of the property in the bean
whose value will be returned when the form is submitted.
labelProperty is the bean property that will be
displayed. I've created a pseudo-property,
fullName, in the Customer bean with a
getter-method that returns the firstName and the
lastName. In the browser we could have this
presented:

The generated HTML looks like this:
<select name="control" size="2">
<option value="001">John Doe</option>
<option value="002" selected="selected">Peter Smith</option>
</select>
An almost identical solution, but using less confusing attribute names, is
this:
<html:select property="control" size="2">
<html:optionsCollection name="customers" value="id" label="fullName"/>
</html:select>
The current value of the list is again set or get in the
execute method--as described previously.
The Selection List - multiple type
You may copy the solutions from the single choice selection list if you add
multiple="true" to the html:select-tag:
<html:select multiple="true" property="control" size="2">
<html:optionsCollection name="customers" value="id" label="fullName"/>
</html:select>
This time, however, you'll have to define the control property in the
ActionForm as a String array:
<form-bean name="multiSelectForm"
type="org.apache.struts.action.DynaActionForm">
<form-property name="control" type="java.lang.String[]"/>
</form-bean>
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.