Struts HTML Checkbox en
Struts HTML Checkbox en
We explain the struts <html:checkbox> element and illustrate the usage with some small examples.
Generals
Author: Sascha Wolski Sebastian Hennebrueder http://www.laliluna.de/tutorials.html Tutorials fr Struts, EJB, xdoclet und eclipse. Date: February 22th 2005 Development Tools Eclipse 3.x
Define a constructor which allows you to set the properties when initializing the class. The object class looks like the following:
public class Customer { private String name; private boolean checked; public Customer(){} public Customer(String name, boolean checked){ this.name = name; this.checked = checked; } public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; } public String getName() { return name; } public void setName(String name) { this.name = name; }
return checked; } public void setChecked(boolean checked) { this.checked = checked; } public Collection getCustomers() { return customers; } public void setCustomers(Collection customers) { this.customers = customers; } /** * reset method * @param mapping * @param request */ public void reset(ActionMapping mapping, HttpServletRequest request) { //reset properties this.checked = false; //initial the customers collection customers = new ArrayList(); customers.add(new Customer("Marie", false)); customers.add(new Customer("Klaus", false)); customers.add(new Customer("Peter", false)); } }
Example 1
The first example shows a checkbox element, which status can be checked and unchecked. The attribute property specifies the associated property checked of the form bean. If the user mark the checkbox and submits the form, the submitted value of the checkbox (on, true, yes) will be converted into a value of type boolean and set in the property checked of the form bean.
<h4>Simple use of <html:checkbox> Tag</h4> <html:checkbox property="checked">Label</html:checkbox> <br /><br /> <html:submit property="btnApply"/>
Example 2
The second example shows the usage of a <html:checkbox> element within a <logic:iterate> tag. The attribute name of the <logic:iterate> element specifies the bean which holds the collection, in our case the form bean exampleForm. The attribute property specifies the name of the collection within the form bean. The attribute id specifies the name of the variable that will contain the current element of the collection on each iteration. Note: In order to refer a property of an element within the collection to a <html:checkbox> element the attribute id of the <logic:iterate> tag must have the same name like the collection of the form bean. Furthermore the attribute indexed of the <html:checkbox> have to be set to true. The value of the attribute name within the <html:checkbox> element refers to the variable which is specified by the attribute id of the <logic:iterate> tag. The attribute property sets the associated property checked of the object class. The indexed attribute of the <html:checkbox> element must be set to true, because the element is inside a <logic:iterate> tag.
<h4>Use of <html:checkbox> Tag inside a <logic:iterate></h4> <logic:iterate name="exampleForm" property="customers" id="customers"> <html:checkbox name="customers" property="checked" indexed="true"> <bean:write name="customers" property="name" /> <br /> </html:checkbox> </logic:iterate> <br /> <html:submit property="btnApply"/>
Now you can test the project. Deploy the project and test it with the following link. http://localhost:8080/CheckboxTag/example.do