我将List作为请求参数从Action类发送到JSP(表单)并使用逻辑迭代器显示此列表。此列表包含具有布尔类型的ActionForm对象(显示为复选框)。
我的要求是所有选中的复选框记录都必须发送回动作类?
请帮助我,过去两天我坚持这一点。
答案 0 :(得分: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页面(这将显示基于传递的List的复选框列表)
<%@ 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>