-1

我将 List 作为请求参数从 Action 类发送到 JSP(form) 并使用逻辑迭代器显示此列表。此列表包含具有布尔类型的 ActionForm 对象(显示为复选框)。

我的要求是所有选中的复选框记录都必须发送回操作类?

请帮助我,过去两天我一直坚持这一点。

4

1 回答 1

0

您可以使用在 JSP 视图中显示多个复选框。

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 页面(这将根据传递的列表显示复选框列表)

<%@ 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(这将显示选定的汽车作为输出)

<%@ 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 文件

<?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>
于 2013-07-02T16:35:30.840 回答