The <html:optionsCollection> tag renders a set of HTML <option> elements, representing possible choices for a <select> element. This tag can be used multiple times within a single <html:select> element, either in conjunction with or instead of one or more <html:option> or <html:options> elements.
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here
Add the following dependencies in pom.xml
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts-core</artifactId> <version>1.3.10</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts-taglib</artifactId> <version>1.3.10</version> </dependency> </dependencies>
1. Create JSP
Now we will create a HTML dropdown using Struts’s HTML tag
Write the following code in dropdown.jsp
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <html> <head> <title>Struts <Html:optionsCollection> OptionsCollection Tag</title> </head> <body> <html:form action="/dropdown"> Fruit : <html:select property="fruit" value="Select"> <html:option value="Select">Select</html:option> <html:optionsCollection name="StrutsDropDownForm" property="fruitList" label="label" value="value" /> </html:select> <html:submit>Submit</html:submit> </html:form> </body> </html>
Now create another JSP output.jsp that displays dropdown value. Write the following code in output.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Output Page</title> </head> <body> Selected Fruit - <bean:write name="StrutsDropDownForm" property="fruit" /> </body> </html>
2. Action Form
Create a Action Form class and name it StrutsDropDownForm.java that contains the following code.
package com.kruders.form; import java.util.ArrayList; import org.apache.struts.action.ActionForm; import org.apache.struts.util.LabelValueBean; public class StrutsDropDownForm extends ActionForm{ private static final long serialVersionUID = 1L; private String fruit; ArrayList fruitList = new ArrayList(); public String getFruit() { return fruit; } public void setFruit(String fruit) { this.fruit = fruit; } public ArrayList getFruitList() { fruitList.add(new LabelValueBean("Apple", "Apple")); fruitList.add(new LabelValueBean("Mango", "Mango")); return fruitList; } public void setFruitList(ArrayList fruitList) { this.fruitList = fruitList; } }
If we have only label and value then we can use the LabelValueBean class to add the label and value to the ArrayList.
fruitList.add(new LabelValueBean("Apple", "Apple")); fruitList.add(new LabelValueBean("Mango", "Mango"));
You can display fruit list in JSP Page like this
<html:optionsCollection name="StrutsDropDownForm" property="fruitList" label="label" value="value" />
We can also create a separate class for Fruit and add it to ArrayList as shown below.
Fruit Class can be like this
public class Fruit { private String fruitId; private String fruitName; public String getFruitId() { return fruitId; } public void setFruitId(String fruitId) { this.fruitId = fruitId; } public String getFruitName() { return fruitName; } public void setFruitName(String fruitName) { this.fruitName = fruitName; } }
You can add Fruit object inside ArrayList like this
fruitList.add(new Fruit("1", "Apple")); fruitList.add(new Fruit("2", "Mango"));
You can display fruit list in JSP Page like this
<html:optionsCollection name="StrutsDropDownForm" property="fruitList" label="fruitName" value="fruitId" />
3. Action Class (Controller)
Let us create an Action class StrutsDropDownAction.java that will handles and forward the request.
package com.kruders.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class StrutsDropDownAction extends Action{ public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return mapping.findForward("success"); } }
4. Struts Config
Write the following code in struts-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="StrutsDropDownForm" type="com.kruders.form.StrutsDropDownForm"/> </form-beans> <global-forwards> <forward name="dropdown" path="/dropdown.do"/> </global-forwards> <action-mappings> <action input="/dropdown.jsp" name="StrutsDropDownForm" path="/dropdown" validate="true" scope="request" type="com.kruders.action.StrutsDropDownAction"> <forward name="success" path="/output.jsp" /> </action> </action-mappings> </struts-config>
5. Deployment Descriptor
Now configure the deployment descriptor. Here, we have asked the container to give our ActionServlet any request that matches the pattern *.do
Add the following configuration information in the web.xml file
<welcome-file-list> <welcome-file>dropdown.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
6. Run Project
Now when you run the project, following screen will be displayed as shown in Figure 30.1
Image may be NSFW.
Clik here to view. Figure 30.1
Now when you select any value in dropdown and click submit button, following screen will be displayed as shown in Figure 30.2
Image may be NSFW.
Clik here to view. Figure 30.2
The folder structure of the example is shown below in Figure 30.3
Image may be NSFW.
Clik here to view. Figure 30.3