Struts HTML MultiBox Tag ( )

SHARE & COMMENT :

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

< html:multibox > -renders a checkbox input field of HTML.Multibox tag is mainly used when more than one checkboxes are used to select.This element is useful when you have large numbers of checkboxes, and prefer to combine the values into a single array-valued property instead of multiple boolean properties. This tag is only valid when nested inside a form tag body. To recognize the selected checkboxes correctly make the corresponding array value denoting the selected values to zero. The selected checkboxes are returned to server depending upon their string values like “yes”,”true”. String[] array is used to hold the list of selected checkboxes.

Example Code for < html:multibox >

1.Create an Jsp page and name it as multibox.jsp.It is the Welcome page for a user.Multibox tag has the same property and “value” attribute should be different which makes the difference to select any one of them.

multibox.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 MultiBox example </title>
    < head>
    < body>
        < h1 > Struts html:multibox example </h1>

        < html:form action="/multi">
            < h5 >< bean:write name="multiboxform" property="error" filter="false"/>

            < table border="0">

                < tbody>
				  < tr>

                        < td> Select Any no of Items < /td>
                    < /tr>
					  < tr>

                        < td> < html:multibox property="item" value="item0"/> Item0< /td>
                    < /tr>
					  < tr>

                        < td> < html:multibox property="item" value="item1"/> Item1< /td>
                    < /tr>
					  < tr>

                        < td> < html:multibox property="item" value="item2"/> Item2< /td>
                    < /tr>
				  < tr>

                        < td> < html:multibox property="item" value="item3"/> Item3< /td>
                    < /tr>
                    < tr>

                        < td> < html:radio property="item" value="item4"/> Item4< /td>
                    < /tr>
                    < tr>

                        < td> < html:radio property="item" value="item5"/> Item5< /td>
                    < /tr>
                < /tbody>
            < /table>

            < html:submit value="Submit"/>
            < html: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.In this example we had properties “item” and “error”.item property is used to hold all the checked items it is of String[] array and error property to hold the error message.In this ActionForm bean we can override the reset() method to evaluate the selected list array values to zero.

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

   private String error;
   private String[] item;
   private int number;

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }


    public String[] getItem() {
        return item;
    }

    public void setItem(String[] item) {
        this.item=new String[item.length];
        this.item = item;
    }
    public String getError() {
        return error;
    }

    public void setError() {
       this.error = "< span style='color:red'> Please check any item< /span>";
    }

    public multiboxform() {
        super();

    }



}

3.Simple Action class multiboxaction.java which is a sub class of Action class used to process the user’s request.In this action class all the validations can be done. In this example checking for the number of checked items.if any one from the list is not selected same page is going to displayed with error message.
multiboxaction.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 multiboxaction extends org.apache.struts.action.Action {


    private final static String SUCCESS = "success";
    private final static String FAILURE = "failure";
     public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        multiboxform formBean=(multiboxform)form;
        String[] values;//=new String[5];
        int count=0;
        values=formBean.getItem();
        if(values==null){
         formBean.setError();
            return mapping.findForward(FAILURE);
        }
        for(int i=0;i< values.length;i++)
        {
                count++;

        }
        formBean.setNumber(count);
        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="multiboxform" type="com.myapp.struts.multiboxform"/>
    </form-beans>

< action-mappings>
        < action name="multiboxform" path="/multi" scope="request" type="com.myapp.struts.multiboxaction" validate="false">
        < forward name="success" path="/multiboxoutput.jsp"/>
        < forward name="failure" path="/multibox.jsp"/>
            </action>
</action-mappings>

	</struts-config>

5.Create another simple Jsp page multiboxoutput.jspwhich is for displaying the output if any of check boxes is checked else return error message in the welcome page itself.

<%@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> MultiBox OutPut page</title>
    </head>
    < body>
        < h3> No of selected Items are: < bean:write name="multiboxform" property="number"/></h3>
    </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

*

Close
Please support the site
By clicking any of these buttons you help our site to get better