Spring Mvc提交/删除表中的已检查(选定)记录

时间:2013-10-21 15:04:33

标签: java jsp spring-mvc

我正在尝试从表中提交所选项目并对它们进行一些修改,但我无法正常工作。

MyObject.java

public class MyObject{
    boolean checkControl = true; //default true
    private String name;
    private String code; 

    //getters & setters
}

MyObjectForm.java

public class MyObjectForm {
    private List<MyObject> myList;

    public List<MyObject> getMyList() {
        return myList;
    }

    public void setMyList(List<MyObject> myList) {
        this.myList= myList;
    }
}

列表myObjects.jsp

<form:form action="submitList" method="post" modelAttribute="myObjectForm">
    <table>
        <tbody>
            <c:forEach items="${myObjectForm.myList}" var="row" varStatus="status">
        <tr>
                <td>
                    <spring:bind path="myList[${status.index}].checkControl">
                    <input type="checkbox" value="<c:out value="${status.value}"/>" name="isChecked" <c:if test="${row.checkControl}"> checked="checked" </c:if> />
                    </spring:bind>
                    </td>
                    <td>${row.name}</td>
                    <td>${row.code}</td>
            </tr>
        </c:forEach>
        </tbody>
    </table>  
    <button type="submit">Submit</button>
</form:form>

和控制器

@RequestMapping(value = "/submitList", method = RequestMethod.POST)
public String save(@ModelAttribute("myObjectForm") MyObjectForm myObjectForm, Model model) {

    List<MyObject> selectedtList = myObjectForm.getMyList(); //returns null

    if (selectedtList == null) {
        System.out.println("no objects selected");
    }
    else {
        //Make some computation
    }
    model.addAttribute("resultArray", selectedtList);

    return "display-items";
}

2 个答案:

答案 0 :(得分:3)

有些用户要求我通过电子邮件更详细地解释这一点。所以我决定在这里提交,希望这会有所帮助。



我正在使用弹簧注释来完成这项工作。这是我处理选中的复选框所做的,

我有一个java实体类,其中包含复选框的布尔值,例如Person类

// Person.java
class Person {
  private Long id;
  private String name;
  private boolean check;

  //here goes getters and setters
}

我在java中有一个表单对象,其中包含Person列表

//PersonForm.java
class PersonForm {
  private List<Person> personList;

  //getters and setters here
}

在我的情况下,有两个jsp页面,第一个列出项目,带有复选框,第二个列出所选项目。

第一个jsp文件是list.jsp

//的List.jsp

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="datatables" uri="http://github.com/dandelion/datatables"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

....

<body>

<form:form action="sent-list" method="post" modelAttribute="personForm">
        <table id="item-list" class="table table-striped table-bordered">
            <thead class="dataTableHeader">
                <tr>
                    <th width="10%" style="text-align:center">
                                     CheckBox
                    </th>
                    <th>id</th>
                    <th>name</th>
                </tr>
            </thead>
            <tbody>
            <c:forEach items="${personForm.personList}" var="listItem" varStatus="status">
                <tr>
                    <td style="text-align:center">
                        <form:checkbox path="listItem[${status.index}].check"/>
                    </td>
                    <td>${listItem.id} <form:hidden path="listItem[${status.index}].id"/></td>
                    <td>${listItem.name} <form:hidden path="listItem[${status.index}].name"/></td>          
                </tr>
            </c:forEach>
            </tbody>
        </table>  
         <button class="btn btn-large btn-success pull-right" type="submit">POST</button>
    </form:form>

</body>
</html>

第二个jsp文件如下

//发送-的List.jsp

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="datatables" uri="http://github.com/dandelion/datatables"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
.....

<body>
<form:form action="#" method="post" modelAttribute="personForm">
        <table id="item-list" class="table table-striped table-bordered">
            <thead class="dataTableHeader">
                <tr>
                    <th>id</th>
                    <th>name</th>
                </tr>
            </thead>
            <tbody>
            <c:forEach items="${resultList}" var="personItem" varStatus="status">
                <tr>
                    <td>${personItem.id}</td>
                    <td>${personItem.name}</td>
                </tr>
            </c:forEach>
            </tbody>
        </table>  
    </form:form>

</body>
</html>

最后有一个控制器,它进行计算

//PersonController.java
@Controller
class PersonController {

    @RequestMapping(value = "/sent-list", method = RequestMethod.POST)
    public String save(@ModelAttribute("personForm") PersonForm personForm, Model model){ 
      for(Person personItem : personForm.getPersonList){
         //make some computation here
      }
    }
}

我希望这会有所帮助。

答案 1 :(得分:2)

听起来像是一个有约束力的问题。您是否尝试过使用Spring的<form:checkbox>代码而不是<spring:bind>?它将自动生成复选框属性,并添加一个隐藏字段,Spring使用该字段来确定复选框是“开”还是“关”。

<form:form action="submitList" method="post" modelAttribute="myObjectForm">
    <table>
        <tbody>
            <c:forEach items="${myObjectForm.myList}" var="row" varStatus="status">
        <tr>
                <td>
                    <form:checkbox path="myList[${status.index}].checkControl"/>
                    </td>
                    <td>${row.name}</td>
                    <td>${row.code}</td>
            </tr>
        </c:forEach>
        </tbody>
    </table>  
    <button type="submit">Submit</button>
</form:form>