Struts HTML Options Collection en
Struts HTML Options Collection en
We explain the Struts <html:optionscollections> 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
Define a constructor which allows you to set the properties when initializing the class. If the attributes value and label of the <html:optioncollection> tag are not used, Struts tries to find a getter method for a value and a label attribute (getValue(), getLabel()). Create both getter methods. The method getValue() returns the property id and the method getLabel() returns the property name of the form bean. The object class looks like the following:
public class Customer { private int id; private String name; public Customer(){} public Customer(int id, String name){ this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLabel(){ return this.name; } public int getValue(){ return this.id; }
We provide some dummy data as collection. The following source code shows the action class:
public class ExampleAction extends Action { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { ExampleForm exampleForm = (ExampleForm) form; //define a dummy collection Collection customers = new ArrayList(); customers.add(new Customer(1, "Marie")); customers.add(new Customer(2, "Klaus")); customers.add(new Customer(3, "Peter")); //set the collection in the request request.setAttribute("customers", customers); return mapping.findForward("success");
<html> <head> <title>example.jsp</title> </head> <body> <html:form action="/example"> .... sample code ... </html:form> </body>
</html>
Example 1
In the first example we output the collection, which was saved in the request (inside the action class). The attribute property of the <html:select> element specifies the associated property selectedItem of the form bean. This property holds the value of the selected option after submitting the form. The attribute name of the <html:optioncollection> tag specifies the name of the collection. We do not use the attributes value and label, so Struts will search for the default getter methods (getValue() and getLabel()). If this methods do not exists an error occurs.
<h4>Simple use of <html:optionsCollection> Tag</h4> <html:select property="selectedItem"> <html:optionsCollection name="customers" /> </html:select> <html:submit/>
Example 2
The next example shows the usage of the attributes value and label. The default getter methods (getValue and getLabel()) do not have to be specified, when the two attributes are set in the <html:optionscollection> tag. Set the attribute label to id and the attribute value to name.
<h4>Use value and label attribute of the <html:options> Tag</h4> <html:select property="selectedItem"> <html:optionsCollection name="customers" label="id" value="name"/> </html:select> <html:submit/>
Now you can test the project. We use a JBOSS or Tomcat installation. Call the project with the following link. http://localhost:8080/OptionsCollectionTag/example.do