2012-10-30 65 views
1

我在動作類中有一個數組列表。我想在我的jsp頁面以及複選框中訪問這個數組列表。如果複選框被選中,我想在另一個actionclass中獲得複選框的關聯值。我怎樣才能做到這一點??在struts中使用複選框和arraylist 2

Action類:

public class CreateModuleAction extends ActionSupport{ 
     private List modNameList=new ArrayList(); 
    private int size; 
public String manageModule() 
    { 

     ManageModule ob=new ManageModule(); 
     modNameList=ob.selectModule(); 

     if(modNameList.isEmpty()) { 
      addActionError("Module list is empty!!!"); 
      return ERROR; 

     } 
     else{ 
     setSize(modNameList.size()); 
     return SUCCESS; 
      } 

    } 
public List getModNameList() { 
     return modNameList; 
    } 

    public void setModNameList(List modNameList) { 
     this.modNameList = modNameList; 
    } 

    public int getSize() { 
     return size; 
    } 

    public void setSize(int size) { 
     this.size = size; 
    } 

jsp頁面:

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@taglib uri="/struts-tags" prefix="s" %> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <link href="../css/style.css" rel="stylesheet" type="text/css" /> 
     <title>Admin Module Management</title> 
    </head> 
    <body> 
      <div class="main"> 
      <div class="header"> 
<!--    Page heading--> 

       <jsp:include page="/templates/heading.jsp" flush="true"/> 
      </div> 
      <div class="menu"> 
<!--    Page menu--> 

<jsp:include page="/templates/menu.jsp" flush="true"/> 
      </div> 
      <div class="content"> 
<!--    page content--> 

       <div id="right_top" style="position:absolute; left:900px; top:300px;;"> 
        <a href="adminCreateModule.jsp"><img src="../images/add.jpg"/>Add</a> 
        <a href="adminEditModule.jsp"><img src="../images/edit.jpg"/>Edit</a> 
        <a href="adminDeleteModule.jsp"><img src="../images/add.jpg"/>Delete</a> 
       </div> 
       <s:iterator status="size" value="modNameList"> 
        <s:checkbox name="modNameCheck" fieldValue="true" value="modNameList"/> 
        <s:property value="modNameList"/> 
       </s:iterator> 
       <s:actionerror/> 

      </div> 
      <div class="footer" style="margin-left: 50%"> 
<!--    page footer--> 

       <jsp:include page="/templates/footer.jsp" flush="true"/> 
      </div> 

     </div> 
    </body> 
</html> 
+0

你嘗試過什麼嗎? – sunleo

+0

我會添加一些我試過的代碼 – user1776488

回答

0

你爲什麼不使用Struts2的checkboxlist tag

<s:form action="myAction"> 
    <s:checkboxlist list="modNameList" name="modNameCheck"/> 
</s:form> 
在yoyr myAction動作類

現在宣佈

private List modNameCheck; //with getter/setter 

這將有哪些是在jsp

0

如果你想從複選框行動提交值檢查條目,那麼你在你的JSP需要form。如果你想顯示選中/取消選中的複選框,取決於某些動作值,那麼你需要在value屬性中進行一些條件檢查。

<s:checkbox name="modNameCheck" value="list.contains(something)"/> 
+0

k.i'l try ... thamx – user1776488