Struts HTML Select Tag ( )

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:select >

< html:select > -renders a select input field of HTML.This tag provides a drop down UI for the user to select an item from the list.It also supports multiple selection of the items by setting multiple attribute to “true”.The “value” attribute of the tag is returned to the server and holds in their respective ActionForm.Select tag can be used with other two html tags like “< options>”,”< optionsCollections>” to implement Collections frame work.

Example Code for < html:select >

1.Create an Jsp page and name it as select.jsp.It is the Welcome page for a user.It Contains the two drop down lists to select the year,month and day.

select.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> Struts HTML Select example </title>
    < head>
    < body>
        < h1 > Struts html:select example </h1>

      < html:form action="/select">
        < table>
            < tr>< td>
                Select year:
            < /td>< td>
                < html:select property="year">
                    < html:option value="">--Select--< /html:option>
                    < html:option value="2005"> 2005< /html:option>
                    < html:option value="2006"> 2006< /html:option>
                    < html:option value="2007"> 2007< /html:option>
                    < html:option value="2008"> 2008< /html:option>
                    < html:option value="2009"> 2009< /html:option>
                    < html:option value="2010"> 2010< /html:option>
                    < /html:select>
            < /td> < /tr>
            < tr> < td>
                Select Month:
                < /td> < td>
< html:select property="month">
                     < html:option value=""> --Select--< /html:option>
                    < html:option value="Jan"> Jan< /html:option>
                    < html:option value="Feb"> Feb< /html:option>
                    < html:option value="Mar"> Mar< /html:option >
                    < html:option value="Apr" > Apr< /html:option >
                    < html:option value="May" > May< /html:option >
                    < html:option value="Jun" > Jun< /html:option >
                    < html:option value="Jul" > Jul< /html:option >
                    < html:option value="Aug" > Aug< /html:option >
                    < html:option value="Sep" > Sep< /html:option >
                    < html:option value="Oct" > Oct< /html:option >
                    < html:option value="Nov" > Nov< /html:option >
                    < html:option value="Dec" > Dec< /html:option >
                    < /html:select >
                < /td >< /tr >
                 < tr >< td >
                Select Day:
                < /td >< td >
< html:select property="day">
                     < html:option value=""> --Select--< /html:option>
                    < html:option value="1"> 1< /html:option>
                    < html:option value="2"> 2< /html:option>
                    < html:option value="3"> 3< /html:option>
                    < html:option value="4"> 4< /html:option>
                    < html:option value="5"> 5< /html:option>
                    < html:option value="6"> 6< /html:option>
                    < html:option value="7"> 7< /html:option>
                    < html:option value="8"> 8< /html:option>
                    < html:option value="9"> 9< /html:option>
                    < html:option value="10"> 10< /html:option>
                    < html:option value="11"> 11< /html:option>
                    < html:option value="12"> 12< /html:option>
                    < html:option value="13"> 13< /html:option>
                    < html:option value="14"> 14< /html:option>
                    < html:option value="15"> 15< /html:option>
                    < html:option value="16"> 16< /html:option>
                    < html:option value="17"> 17< /html:option>
                    < html:option value="18"> 18< /html:option>
                    < html:option value="19"> 19< /html:option>
                    < html:option value="20"> 20< /html:option>
                    < html:option value="21"> 21< /html:option>
                    < html:option value="22"> 22< /html:option>
                    < html:option value="23"> 23< /html:option>
                    < html:option value="24"> 24< /html:option>
                    < html:option value="25"> 25< /html:option>
                    < html:option value="26"> 26< /html:option>
                    < html:option value="27"> 27< /html:option>
                    < html:option value="28"> 28< /html:option>
                    < html:option value="29"> 29< /html:option>
                    < html:option value="30"> 30< /html:option>
                    < html:option value="31"> 31< /html:option>
                   < /html:select>
                < /td> < /tr>
                < tr> < td> < html:submit value="Submit"/> < /td> < /tr>
        < /table>
        < /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.In this example we had properties “year”,”month” and “day”.In this ActionForm bean we can override the reset() method to evaluate the selected list array values to zero.

selectform.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 selectform extends org.apache.struts.action.ActionForm {


     private String year;
      private String month;
       private String day;

    public String getDay() {
        return day;
    }

    public void setDay(String day) {
        this.day = day;
    }

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }




    public selectform() {
        super();

    }


}

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="selectform" type="com.myapp.struts.selectform"/>
    </form-beans>

< action-mappings>
        < action name="selectform" path="/select" scope="request" type="com.myapp.struts.selectaction" validate="false">
        < forward name="success" path="/selectoutput.jsp"/>

            </action>
</action-mappings>

	</struts-config>

5.Create another simple Jsp page selectoutput.jspwhich is for displaying the date as output for selected else displays error message.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>

< html>
    < head>
        < meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        < title> Select OutPut page</title>
    </head>
    < body bgcolor="DDDDDD">
        < b> Selected Date is :
		< bean:write name="selectform" property="day"/>
		< bean:write name="selectform" property="month"/>
		< bean:write name="selectform" property="year"/>
		</b>
    </body>
</html>

6.Building and running the application
Output

Access page:http://localhost:8080/multibox/

also read:

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*