Search code examples
jspcheckboxparameter-passingstruts-1

How to pass List from JSP to Struts1.2 action class?


I am sending List as request parameter from Action class to JSP(form) and displaying this list using logic iterator. This list contains ActionForm objects which has boolean type(displaying as checkbox).

My requirement is all selected checkbox records have to send back to action class?

Pleas help me i have stuck with this from past two days.


Solution

  • you can use to display multiple check boxes in your JSP view.

    CheckBoxListAction.java

    import java.util.ArrayList;
    import java.util.List;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class CheckBoxListAction extends ActionSupport{
        //this list will be passed to jsp view
        private List<String> cars;
        //this variable will hold the selected car references
        private String yourCar;
    
        public CheckBoxListAction(){
            cars = new ArrayList<String>();
            cars.add("toyota");
            cars.add("nissan");
            cars.add("volvo");
            cars.add("honda");
        }
    
        public String getYourCar() {
            return yourCar;
        }
    
        public void setYourCar(String yourCar) {
            this.yourCar = yourCar;
        }
    
        public List<String> getCars() {
            return cars;
        }
    
        public void setCars(List<String> cars) {
            this.cars = cars;
        }
    
        public String execute() {
            return SUCCESS;
        }
    
        public String display() {
            return NONE;
        }
    }
    

    checkBoxList.jsp page (this will display the list of checkboxes based on the List passed)

    <%@ taglib prefix="s" uri="/struts-tags" %>
    <html>
    <head>
    </head>
    
    <body>
    <h1>Struts 2 multiple check boxes example</h1>
    
    <s:form action="resultAction" namespace="/">
    
    <h4>
        <s:checkboxlist label="What's your dream car" list="cars" 
           name="yourCar" />
    </h4> 
    
    <s:submit value="submit" name="submit" />
    
    </s:form>
    
    </body>
    </html>
    

    selectedCars.jsp (this will display the selected cars as the output)

    <%@ taglib prefix="s" uri="/struts-tags" %>
    <html>
    
    <body>
    <h1>Struts 2 multiple check boxes example</h1>
    
    <h4>
      Dream Car : <s:property value="yourCar"/>
    </h4> 
    
    </body>
    </html>
    

    struts.xml file

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <struts>
    
     <constant name="struts.devMode" value="true" />
    
    <package name="default" namespace="/" extends="struts-default">
    
       <action name="checkBoxListAction" 
             class="com.mkyong.common.action.CheckBoxListAction" method="display">
        <result name="none">pages/checkBoxList.jsp</result>
       </action>
    
       <action name="resultAction" class="com.mkyong.common.action.CheckBoxListAction">
        <result name="success">pages/selectedCars.jsp</result>
       </action>
      </package>
    
    </struts>