Struts HTML Tag Library
Struts HTML tag library provides tags which are used to create input forms and HTML user interfaces. The tags in the Struts HTML library form a bridge between a JSP view and the other components of a Web application. Since a dynamic Web application often depends on gathering data from a user, input forms play an important role in the Struts framework. Consequently, the majority of the HTML tags involve HTML forms.
Syntax to use Struts HTML tag library
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
< html:optionsCollection >
< html:optionsCollection > -renders a HTML option field.This Tag is valid only if nested inside < select> Tag.The element is used to display data of lists (arrays, collections) inside a select element.This tag operates on a collection of beans, where each bean has a label property and a value property.This tag differs from the < html:options> tag in that it makes more consistent use of the name and property attributes, and allows the collection to be more easily obtained from the enclosing form bean.
Example Code for < html:optionsCollection>
1.Create an Jsp page and name it as example.jsp.It is the Welcome page for a user. In this example the options in drop down are loaded dynamically from Collection countrylist(ArrayList).
example.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html" %> <%@taglib prefix="bean" uri="http://jakarta.apache.org/struts/tags-bean" %> < html> < head> < title> HTML OptionsCollection example </title> < head> < body bgcolor="#DDDDDD"> < h1 > Struts html:optionsCollection example </h1> < html:form action="/country" > < h1> Select Country: < html:select name="CountryForm" property="country"> < html:option value="0">--Select Country--< /html:option> < html:optionsCollection name="CountryForm" property="countryList" label="countryname" value="countryid" /> < /html:select> < /h1> < html:submit value="save" property="method"/>< html:reset value="Reset"/> < /html:form> </body> </html>
2.Create a Form bean.Form bean is used to hold the properties of the submitted form which is a sub class of ActionForm.Here we have two properties to hold in tha form bean “countrylist” and “country” of type ArrayList(Collection),String respectively.
CountryForm.java
package com.myapp.struts; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; public class CountryForm extends org.apache.struts.action.ActionForm { private String country; private ArrayList countryList; public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public ArrayList getCountryList() { return countryList; } public void setCountryList(ArrayList countryList) { this.countryList = countryList; } public CountryForm() { super(); } }
3.Simple Action class CountryAction.java which is a sub class of Action class used to load the options values into the dropdown list provided to the user. country class objects are added to the ArrayList countrylist and set to the CountryForm’s setCountryList(). It contains a method called save() which is called when the form is submitted.
CountryAction.java
package com.myapp.struts; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForward; public class CountryAction extends org.apache.struts.action.Action { private final static String SUCCESS = "success"; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { CountryForm formBean=(CountryForm)form; ArrayList countrylist=new ArrayList(); ArrayList statelist=new ArrayList(); countrylist.add(new country("1","INDIA")); countrylist.add(new country("2","USA")); countrylist.add(new country("3","CHINA")); countrylist.add(new country("4","CANADA")); formBean.setCountry(""); formBean.setCountryList(countrylist); return mapping.findForward(SUCCESS); } public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return mapping.findForward(SUCCESS); } }
4.Create or modify struts config file struts-config.xml with action mappings.Struts-config file contains the information about the configuration of the struts framework to the application.It contains the action mappings which helps to select Action,ActionForm and other information for specific user’s request’s.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> < struts-config> < form-beans> < form-bean name="CountryForm" type="com.myapp.struts.CountryForm"/> </form-beans> < action-mappings> < action input="/example.jsp" name="CountryForm" path="/country" scope="request" type="com.myapp.struts.CountryAction" validate="false"> < forward name="success" path="/example.jsp"/> </action> </action-mappings> </struts-config>
5.Create another simple Jsp page country.javawhich is a java class used to create the country objects to add to the list. the attribute propery label and value for the option in drop down list are the countryid and countryname from this object
public class country { private String countryid; private String countryname; public country(String countryid,String countryname) { this.countryid=countryid; this.countryname=countryname; } public String getCountryid() { return countryid; } public void setCountryid(String countryid) { this.countryid = countryid; } public String getCountryname() { return countryname; } public void setCountryname(String countryname) { this.countryname = countryname; } }
6.Change or modify the web.xml file by making the welcome file to index.jsp which contains the jsp forward to call action method to load the drop down list data.
index.jsp
<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> < jsp:forward page="country.do"/>
7.Building and running the application
Output
Access page:http://localhost:8084/optionscollection/
also read: