2015-10-15 82 views
0

我有一個包含函數列表的jsp頁面。在控制器中,我從數據庫中獲取這個列表並將它傳遞給jsp。如何在Spring MVC中將複選框值傳遞給控制器​​

@RequestMapping(value = "/functionlist", method = RequestMethod.GET) 
    public ModelAndView functionList(Model model) throws Exception { 
     ModelAndView mv = new ModelAndView("functionList"); 
     mv.addObject("functionList", getFunctionsFromDB()); 
     return mv; 
    } 

在我的jsp頁面中創建表使用的功能

<table id="table"> 
    <thead> 
    <tr> 
     <th></th> 
     <th>Name</th> 
     <th>Action</th> 
    </tr> 
    </thead> 
    <tbody> 
    <c:choose> 
     <c:when test="${not empty functionList}"> 
      <c:forEach var="function" items="${functionList}"> 
       <tr> 
        <td><input name="id" value="${function.id}" hidden></td> 
        <td>${function.name}</td> 
        <td> 
         <input type="checkbox" id="${function.id}" value="${function.action}"></td> 
       </tr> 
      </c:forEach> 
     </c:when> 
    </c:choose> 
    </tbody> 
</table> 
<button type="submit" name="save">Save</button> 

我也給功能ID來複選框ID這個名單。

我的功能實體是以下

public class Function { 

    private Integer id; 
    private String name; 
    private Boolean action; 
... 
} 

我想按按鈕保存並在控制器得到「/ functionlist /保存」複選框我值列表。

+0

這種情況在這裏描述:https://stackoverflow.com/questions/2060839/spring-mvc-and-checkboxes –

回答

4

嘗試添加form像這樣JSP頁面

<form:form id="yourForm" action="/functionlist/save" method="POST" modelAttribute="functionList"> 

     <c:forEach items="${functionList}" varStatus="status" var="function"> 

      <tr> 
       <td>${function.name}</td> 
       <td> 
        <form:checkbox path="functionList[${status.index}].action"/> 
       </td> 
      </tr> 
     </c:forEach> 

     <input type="submit" value="submit" /> 
    </form:form> 

和控制器,你應該有這樣的

@RequestMapping(value = { "/functionlist/save" }, method = RequestMethod.POST) 
    public String savePerson(@ModelAttribute("functionList")List<Function> functionList) { 
     // process your list 
    } 
的方法

如果這不起作用,您可以嘗試包裹您的列表。

public class FunctionListWrapper { 
    private List<Function> functionList; 

    public FunctionListWrapper() { 
     this.functionList = new ArrayList<Function>(); 
    } 

    public List<Function> getFunctionList() { 
     return functionList; 
    } 

    public void setFunctionList(List<Function> functionList) { 
     this.functionList = functionList; 
    } 

    public void add(Function function) { 
     this.functionList.add(function); 
    } 
} 

在控制器強似列表,通過包裝

FunctionListWrapper functionListWrapper=new FunctionListWrapper(); 
     functionListWrapper.setFunctionList(userService.getFunctionList()); 
     mv.addObject("functionListWrapper", functionListWrapper); 

欲瞭解更多詳情,請看一看這個問題:question 1question 2

1

首先,你需要name屬性添加到您的複選框inputs.You可以 得到這些與同名控制器陣列,您的姓名attribute.eg-

@RequestMapping(value = "/functionlist", method = RequestMethod.GET) 
     public ModelAndView functionList(Model model,@RequestParam("checkboxname")String[] checkboxvalues) throws Exception { 
     ModelAndView mv = new ModelAndView("functionList"); 
     mv.addObject("functionList", getFunctionsFromDB()); 
     return mv; 
} 
0

幾件事情。 首先你應該考慮一個表單對象,它是視圖和控制器之間的交換實體。表單實體將類似於followng東西:

public class Form { 
private List<Function> functions; 
//getters & setters 
} 

其次,因爲你正在使用Spring MVC的,你應該充分利用<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>庫。因此,你的表格是:

<form:form commandName="form" action="/your/url"> 
<c:forEach items="${form.functions }" var="function" varStatus="status"> 
    <tr> 
     <td><form:hidden path="functions[${status.index }].id"></td> 
     <td>${function.name}</td> 
     <td> 
      <form:checkbox path="functions[${status.index }].action" id="${function.id}" value="${function.action}"/> 
     </td> 
    </tr> 
</c:forEach> 
</form:form> 

終於控制器會像

@RequestMapping(value="/your/url", method=RequestMethod.POST) 
public String postForm(@ModelAttribute("form") FormForm form) 
{ 
    //cool stuff here 
} 

請注意適當的HTTP方法是POST,沒有得到。是的,我知道事情也與GET一起工作,這絕對是一個被棄用的策略。 另外,要弄清楚我是如何參考Form中的Function列表。當我必須顯示數據時,我在foreach聲明中使用了變量function,我在必須綁定Form對象以將數據傳輸到控制器時參考了列表。最後但並非最不重要的是,我沒有嘗試過這些代碼。我只是爲了給你提示如何正確使用它而寫的。 最後一件事。由於您將表單傳遞給JSP,您不再需要使用您使用的屬性填充模型。只需用數據填寫表單,然後用它在視圖中顯示它們。就像這樣${form.stuff.you.want}

相關問題