With the following code:
Code:
@RequestMapping(value = "adminUsers", method = RequestMethod.GET)
protected ModelAndView listAdminUsers() throws Exception {
Person person = new Person();
ModelAndView mav = new ModelAndView("adminUsersList");
mav.addObject("adminUsersList", adminService.findAllAdminUsers());
mav.addObject("person", person);
return mav;
}
// Create a new item
@RequestMapping(value="adminUsers", method = RequestMethod.POST)
public ModelAndView listAdminUsers(Person newPerson, HttpServletResponse response) throws Exception {
Person person = personService.findPerson(newPerson.getUsername());
if (person == null) {
ModelAndView mav = new ModelAndView("adminUsersList"); //This is ugly, it's copy & pasted from above
mav.addObject("adminUsersList", adminService.findAllAdminUsers()); //Ugly...
// How to add error message though?
return mav;
} else {
Roles roles = new Roles();
roles.setPersonCode(person.getPersonCode());
roles.setRoleType("ADMN");
rolesMapper.insert(roles);
return new ModelAndView("redirect:/admin/adminUsers.html");
}
}
Code:
<form:form method="post" action="${action}" commandName="person" >
<form:label path="username">Add new administrator:</form:label>
<form:input path="username" size="20"/>
<form:errors path="username" />
<input type="submit" value="Submit Changes"/>
</form:form>
<table>
<c:forEach var="user" items="${adminUsersList}">
<tr>
<td>
${user.title} <c:choose><c:when test="${not empty user.knownAs}">${user.knownAs}</c:when><c:otherwise> ${user.forename}</c:otherwise></c:choose> ${user.surname}
</td>
<td>
${user.emailAddress}
</td>
</tr>
</c:forEach>
</table>
If there is an error, (e.g. if (person == null) ) what is the best way to redisplay the form with:
- The input box filled in
- An error message
- The contents of adminService.findAllAdminUsers()
I've marked //ugly where I've just copied and pasted code. Although it's only 2 lines here, it could be a lot more complex and I don't like the feel of it. I also can't see how to bind an error message so it gets displayed in the <form:errors path="username" /> tag.
Thanks!