how to update only modified values in spring mvc from spring form

风流意气都作罢 提交于 2019-12-25 03:42:48

问题


I am displaying checkboxes on a page and want to only update values in the db for which the values were changed. Also I would like to display an error if nothing was changed and form was submitted.

This is what i have so far. Any suggestion?

View

public class PersonView {

    private List<Person> personList;

    public List<Person> getPersonList() {
        return personList;
    }

    public void setPersonList(List<Person> personList) {
        this.personList = personList;
    }
}

Domain

public class Person {

    private String fullName; 
    private Boolean isSupervisor;
    private Boolean isManager;

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public Boolean isSupervisor() {
        return isSupervisor;
    }

    public void setSupervisor(Boolean isSupervisor) {
        this.isSupervisor = isSupervisor;
    }

    public Boolean isManager() {
        return isManager;
    }

    public void setManager(Boolean isManager) {
        this.isManager = isManager;
    }
}

Controller

@Controller
@RequestMapping("/person.html")
public class PersonsController {

    @Autowired
    private PersonService personService;

    @RequestMapping(method = RequestMethod.GET)
    public String initForm() {
        return "member";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String submitForm(@ModelAttribute PersonView personView) {
        model.addAttribute("persons", personView);
        return "successMember";
    }

    @ModelAttribute
    public PersonView getPersonView(){
        List<Person>  persons= personService.getPersonList();
        PersonView pv = new PersonView();
        pv.setPersonList(persons);

        return pv;
    }
}

JSP

<html>
<title>Persons Information</title>
</head>

<body>

    <form:form method="POST" modelAttribute="personView">
        <table>
            <tr>
                <th>Full Name</th>
                <th>Supervisor</th>
                <th>Manager</th>
            </tr>

            <c:forEach var="person" items="${personView.personList}"
                varStatus="row">
                <tr>
                    <td>{person.fullName}</td>
                    <td><form:checkbox path="personList[row.index].supervisor"  value="true" />
                    </td>
                    <td><form:checkbox path="personList[row.index].manager"     value="true" />
                    </td>
                </tr>
            </c:forEach>


            <tr>
                <td><input type="submit" name="submit" value="Submit">
                </td>
            </tr>
            <tr>
        </table>
    </form:form>

</body>
</html>

来源:https://stackoverflow.com/questions/29978812/how-to-update-only-modified-values-in-spring-mvc-from-spring-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!