Struts <html:multibox> Tag

The <html:multibox> tag is used to display multiple checkbox elements. It is mainly used when you have to create group of check boxes dynamically at run time. Instead of using several instance of checkbox tag. In order to use the Struts HTML Tags you need to include the following taglib directive in the jsp page.

<%@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 group of HTML checkbox using Struts’s HTML tag

Write the following code in multibox.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>Struts <Html:multibox> Multibox</title>
</head>
<body>
<html:form action="/multibox">
    Your Hobbies
    <logic:iterate id="hobby" name="StrutsMultiBoxForm" property="hobbies">
        <html:multibox  property="selectedHobbies">
            <bean:write name="hobby"/>
        </html:multibox>
        <bean:write name="hobby"/>
    </logic:iterate>
    <html:submit>Submit</html:submit>
</html:form>
</body>
</html>


Instead of using several instance of checkbox tag, we can use multibox tag in conjunction with struts <logic:iterate> tag.

Now create another JSP output.jsp that display the selected checkbox value. Write the following code in output.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>Output Page</title>
</head>
<body>
<html:form>
    You have selected : 
    <logic:iterate id="hobby" name="StrutsMultiBoxForm" property="hobbies">
        <<html:multibox  property="selectedHobbies">
            <bean:write name="hobby"/>
        </html:multibox>
        <bean:write name="hobby"/>
    </logic:iterate>
</html:form>
</body>
</html>

2. Action Form

Create a Action Form class and name it StrutsMultiBoxForm.java that contains the following code.

package com.kruders.form;
 
import org.apache.struts.action.ActionForm;
 
public class StrutsMultiBoxForm extends ActionForm{
    private static final long serialVersionUID = 1L;
    private String [] hobbies = {"Music","Gardening", "Photography"}  ;
    private String [] selectedHobbies = {"Music"}  ;
     
    public String[] getHobbies() {
        return hobbies;
    }
    public void setHobbies(String[] hobbies) {
        this.hobbies = hobbies;
    }
    public String[] getSelectedHobbies() {
        return selectedHobbies;
    }
    public void setSelectedHobbies(String[] selectedHobbies) {
        this.selectedHobbies = selectedHobbies;
    }
}

3. Action Class (Controller)

Let us create an Action class StrutsMultiBoxAction.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 StrutsMultiBoxAction 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="StrutsMultiBoxForm" type="com.kruders.form.StrutsMultiBoxForm"/>
    </form-beans>
     
    <global-forwards>
        <forward name="multibox" path="/multibox.do"/>
    </global-forwards>
     
    <action-mappings>
        <action input="/multibox.jsp" name="StrutsMultiBoxForm" path="/multibox"  validate="true" scope="request" type="com.kruders.action.StrutsMultiBoxAction">
            <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>multibox.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 28.1

Figure 28.1 Figure 28.1


Now when you select any checkbox and click submit button, following screen will be displayed as shown in Figure 28.2

Figure 28.2 Figure 28.2

The folder structure of the example is shown below in Figure 28.3

Figure 28.3 Figure 28.3

You can download the source code of this example here.

,

One Response to Struts <html:multibox> Tag

  1. Bruno October 23, 2014 at 12:11 am #

    Very useful! Helped me to solve a problem on a legacy system after almost ten hours of unsuccessful approachs. Thank you very much sir.

Leave a Reply