Bean Validation - Edit Account
UsersController.java
RequestMapping(value="/editAccount",method=RequestMethod.POST) public String editAccount(@Valid User user, BindingResult bindingResult, Principal principal) { if (bindingResult.hasErrors()) { return "users/editAccount"; } //user.setEmail(principal.getName()); int check = userService.editAccount(user); if (check != 1) { throw new RuntimeException(WebContants.EDIT_ACCOUNT_FAIL); } return "redirect:/users/changePasswd"; }
editAccount.jsp
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf" %> <sf:form id="editAccountForm" action="editAccount" method="post" modelAttribute="user" onsubmit="return check();"> <sf:errors path="*" cssClass="error" /> <table> <tr> <td>Full Name</td> <td> <sf:input path="name" /><br /> <sf:errors path="name" cssClass="error" /> </td> </tr> <tr> <td>Mobile</td> <td> <sf:input path="mobile" /><br /> <sf:errors path="mobile" cssClass="error" /> </td> </tr> <tr> <td>Password</td> <td> <sf:password path="passwd" /><br /> <sf:errors path="passwd" cssClass="error" /> </td> </tr> <tr> <td colspan="2"><input type="submit" value="Submit" /></td> </tr> </table> </sf:form>
Test
After logging in, remove all existing values from the account modification form page and click the Submit button.
References
- Getting started with Hibernate Validator
- Validation - Empty int field
- http://stackoverflow.com/questions/14715248/simple-springmvc-3-login-doesnt-work
- http://stackoverflow.com/questions/6227547/spring-3-validation-not-working
- http://stackoverflow.com/questions/8909482/spring-mvc-3-ambiguous-mapping-found
- spring-framework-reference.pdf
- MyBatis Getting started