You are on page 1of 3

Struts Code Peaces <html:multibox> Element

We explain the Struts <html:multibox> element and illustrate the usage with some small examples.
Generals
Author:
Sascha Wolski
Sebastian Hennebrueder
http://www.laliluna.de/tutorials.html Tutorials !r Struts" #$%" xdoclet und eclipse.
Date:
&ebruar' ((th ())*
The <html:multibox> element
The <html:multibox> element is used to manage multiple checkbox elements" or example in lists.
The +alue" which will be transered to the ser+er rom a selected checkbox" can be an' +alue o
t'pe string. The associated propert' in the orm,bean is an arra' o t'pe string" which holds a list
o selected +alues ater submitting the orm.
The ollowing examples shows the source code o the $S- ile:
<html:multibox property="selectedItems" value="1"> Maria
<html:multibox property="selectedItems" value="2"> Klaus
The ollowing HT./ source code is rendered at runtime:
<input type="checkbox" name="selectedItems" value="1"> Maria
<input type="checkbox" name="selectedItems" value="2"> Klaus
Attributes of the <html:multibox> element
0ow the most important attributes will be explained. 1ou ind a complete list o all a+ailable
attributes or this tag in the 2-3 o the HT./ tag librar'.
http://struts.apache.org/user4uide/struts,html.html
Name eschreibun!
disabled 5isables the element 6true / alse7
name 0ame o the bean which contains the properties.
propert' 0ame o the propert' or re8uest parameter" which is associated with this element.
+alue
The +alue which is submitted to the ser+er. 3 not speciied the +alue inside the bod' o
this tag will be used.
"sa!e of the <html:multibox> element
9reate a new struts pro:ect to illustrate the usage o the <html:multibox> element.
Create a ne# action class
9reate a new action class ExampleAction in the package de.laliluna.tutorial.multibox.action.
We use the class LabelValueBean to add a label and a +alue to the collection.
public class xample!ction extends !ction "
public !ction#or$ard execute%
!ctionMappin& mappin&'
!ction#orm (orm'
)ttp*ervlet+e,uest re,uest'
)ttp*ervlet+esponse response- "
xample#orm example#orm = %xample#orm- (orm.
//initial the customers collection
0ollection customers = ne$ !rray1ist%-.
customers2add%ne$ 1abel3alue4ean%"Marie"' "1"--.
customers2add%ne$ 1abel3alue4ean%"Klaus"' "2"--.
customers2add%ne$ 1abel3alue4ean%"5eter"' "6"--.
//set customer collection in the re,uest
re,uest2set!ttribute%"customers"' customers-.
return mappin&2(ind#or$ard%"success"-.
7
7
Create a ne# form bean
9reate a new action orm class ExampleForm in the package de.laliluna.tutorial.multibox.form.
5eine a propert' arra' selectedItem o t'pe String. This propert' holds the +alues o the
associated multibox elements.
-ro+ide a getter and setter method or the propert'.
The ollowing source code shows the action orm class:
public class xample#orm extends !ction#orm "
private *trin&89 selectedItems = "7.
public *trin&89 &et*electedItems%- "
return selectedItems.
7
public void set*electedItems%*trin&89 selectedItems- "
this2selectedItems = selectedItems.
7
7
Create the struts$confi!%xml
;pen the struts,conig.xml and deine the orm bean and action mapping.
<struts:con(i&>
<(orm:beans>
<(orm:bean name="example#orm"
type="de2laliluna2tutorial2multibox2(orm2xample#orm" />
</(orm:beans>
<action:mappin&s>
<action
name="example#orm"
path="/example"
scope="re,uest"
type="de2laliluna2tutorial2multibox2action2xample!ction">
<(or$ard name="success" path="/(orm/example2;sp" />
</action>
</action:mappin&s>
</struts:con(i&>
Create the &s' file
9reate a $S- ile named example.jsp in the older /eb!oot/form/.
;pen the $S- ile example.jsp and add the ollowing source code.
<<= pa&e lan&ua&e=";ava"<>
<<= ta&lib uri="http://;akarta2apache2or&/struts/ta&s:bean" pre(ix="bean"<>
<<= ta&lib uri="http://;akarta2apache2or&/struts/ta&s:html" pre(ix="html"<>
<<= ta&lib uri="http://;akarta2apache2or&/struts/ta&s:lo&ic" pre(ix="lo&ic"<>

<html>
<head>
<title>example2;sp</title>
</head>
<body>
<html:(orm action="/example">
2222 sample code 222
</html:(orm>
</body>
</html>
Within the <html:form> element add the irst example.
Exam'le (
There are three <html:multibox> elements in the irst example. The attribute propert" o the
element reers to the propert' selectedItems o the orm bean. The attribute +alue speciies the
+alue o each element" which are submitted to the ser+er.
<h>>*imple use o( ?lt.html:multibox?&t. ta&s</h>>
<html:multibox property="selectedItems" value="!" /> Maria
<html:multibox property="selectedItems" value="K" /> Klaus
<html:multibox property="selectedItems" value="5" /> 5eter
<br />
<html:submit property="btn!pply"/>
Exam'le )
The example ( shows the usage o an <html:multibox> element inside an iteration <lo#ic:iterate>.
3nside the iteration the +alue o the current element o the collection is assigned to the
<html:multibox> element. <bean:$rite ..> inside the bod' o the checkbox element sets the +alue "
which is submitted to the ser+er. The label is set with a <bean:$rite ...> ater the checkbox
element. The attribute propert" o the mulitbox element speciies the associated propert'
selectedItem o the orm bean.
<h>>@se o( ?lt.html:multibox?&t. ta& inside a ?lt.lo&ic:iterate?&t.</h>>
<lo&ic:iterate name="customers" id="customer">
<html:multibox property="selectedItems">
<bean:$rite name="customer" property="value" />
</html:multibox>
<bean:$rite name="customer" property="label" />
</lo&ic:iterate>
<br />
<html:submit property="btn!pply"/>
0ow 'ou can test the pro:ect. We use a $%;SS or Tomcat installation. 9all the pro:ect with the
ollowing link.
http://localhost:<)<)/.ultiboxTag/example.do

You might also like