最近学习了一下springmvc中后台校验功能的实现,特别写了一个demo以备以后查看,主要使用注解方式来进行配置。
1、在jsp页面引入spring的标签
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
2、建立数据提交表单
<form:form method="post" action="reg.do" commandName="userBean"> <table> <tr> <td> UserName: <font color="red"> <form:errors path="userName"></form:errors> </font> </td> </tr> <tr> <td> <form:input path="userName"/> </td> </tr> <tr> <td> Age: <font color="red"> <form:errors path="age"></form:errors> </font> </td> </tr> <tr> <td> <form:input path="age"/> </td> </tr> <tr> <td> password: <font color="red"> <form:errors path="password"></form:errors> </font> </td> </tr> <tr> <td> <form:password path="password"/> </td> </tr> <tr> <td> Confirm Password: <font color="red"> <form:errors path="confirmPassword"></form:errors> </font> </td> </tr> <tr> <td> <form:password path="confirmPassword"/> </td> </tr> <tr> <td> <input type="submit" value="submit"> </td> </tr> </table> </form:form>
3、新建表单对象(javabean)
package springapp.model; import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import org.hibernate.validator.constraints.NotEmpty; import org.springframework.format.annotation.NumberFormat; import org.springframework.format.annotation.NumberFormat.Style; public class UserBean { @NotEmpty @Size(min = 1, max = 20) private String userName; @NumberFormat(style = Style.NUMBER) @NotNull(message = "Age must not be blank") @Min(value = 1, message = "Age must more then 1") @Max(value = 100, message = "Age must less then 100") private Integer age; @NotEmpty(message = "Password must not be blank.") @Size(min = 1, max = 10, message = "Password must between 1 to 10 Characters.") private String password; @NotEmpty private String confirmPassword; /** get and set **/ }
在对表单对象的错误信息提示的时候 我采用了几种方式。userName的错误信息配置在资源文件中(message.properties),如下所示:
NotEmpty.userBean.userName=\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A #用户名不能为空
如果需要把错误提示信息配置在资源文件中,需要在spring-servlet.xml文件对 ReloadableResourceBundleMessageSource这个类进行注册如下:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="/WEB-INF/message" /> </bean>
Age和password的提示信息就直接写在UserBean中,比如:
@NumberFormat(style = Style.NUMBER) @NotNull(message = "Age must not be blank") @Min(value = 1, message = "Age must more then 1") @Max(value = 100, message = "Age must less then 100") private Integer age;
对password与Confirm password的校验就写在校验器中。如
package springapp.action; import org.springframework.stereotype.Component; import org.springframework.validation.Errors; import org.springframework.validation.ValidationUtils; import springapp.model.UserBean; @Component("regValidation") public class RegValidation { public boolean supports(Class<?> clazz) { return UserBean.class.isAssignableFrom(clazz); } public void validate(Object target, Errors errors) { UserBean userBean = (UserBean) target; ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName", "NotEmpty.userBean.userName", "User Name must not be Empty."); String userName = userBean.getUserName(); if (!(userBean.getPassword()).equals(userBean.getConfirmPassword())) { errors.rejectValue("password", "matchingPassword.userBean.password", "Password and Confirm Password Not match."); } } }
5、新建处理表单的Action
package springapp.action; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import springapp.model.UserBean; @Controller public class UserAction { @RequestMapping(value = "reg.do", method = RequestMethod.GET) public String init(Model model) { UserBean useBean = new UserBean(); model.addAttribute("userBean", useBean); return "reg"; } @RequestMapping(value="reg.do",method=RequestMethod.POST) public String reg(Model model,@Valid UserBean userBean,BindingResult result) { regValidation.validate(userBean, result); if(result.hasErrors()) { return "reg"; } model.addAttribute("userBean", userBean); return "regSuccess"; } @Autowired private RegValidation regValidation; }