2013-07-02 115 views
-1

我從Action類發送List as request參數到JSP(表單)並使用邏輯迭代器顯示此列表。該列表包含具有布爾類型的ActionForm對象(顯示爲複選框)。如何將List從JSP傳遞到Struts1.2動作類?

我的要求是所有選中的複選框記錄必須發回操作類?

請幫助我,我一直堅持這從過去兩天。

+1

請將您的代碼張貼在[SSCCE](http://sscce.org)表單中。這是一個獲取技術幫助的網站,而不是最終用戶的幫助。 –

回答

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>