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>