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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19498087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:46:08  来源:igfitidea点击:

Spring Mvc submit/delete checked (selected) records from table

javajspspring-mvc

提问by fsonmezay

I'm trying to submit selected items from a table and moake some modifications on them but I couldn't get it work.

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

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;
    }
}

list-myObjects.jsp

列表-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>

And the controller

和控制器

@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";
}

采纳答案by Will Keeling

Sounds like a binding issue. Have you tried using Spring's <form:checkbox>tag rather than <spring:bind>? It will automatically generate the checkbox attributes as well as adding a hidden field that Spring uses to determine whether the checkbox is 'on' or 'off'.

听起来像是一个绑定问题。您是否尝试过使用 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>

回答by fsonmezay

Some users asked me to explain this in more detail by email. So I decided to submit it here, Hope this helps.

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





I'm using spring annotations, to do the job. here is what I did to process selected checkboxes,

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

I have a java entity class which includes a boolean value for the checkbox, for example a Person class

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

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

  //here goes getters and setters
}

than I have a form object in java, which contains a list of Person

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

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

  //getters and setters here
}

in my case, there are two jsp pages, the first one lists items, with checkboxes, and the second one lists selected items.

在我的例子中,有两个 jsp 页面,第一个列出带有复选框的项目,第二个列出选定的项目。

the first jsp file is list.jsp

第一个jsp文件是list.jsp

//list.jsp

//列表.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>

the second jsp file is as follows

第二个jsp文件如下

//sent-list.jsp

//发送列表.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>

and finally there is a controller, which makes the computation

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

//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
      }
    }
}

I hope this helps.

我希望这有帮助。