Use hibernate-validator in ssm to verify BO, hibernatevalidator
Popular verification practices: front-end jquery-form-validate + backend hibernate-validate
Add relevant jar in pom:
<!-- use hibernate-validator to validate entity before enter controller --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>${validation.api.version}</version> </dependency>
Configuration in spring mvc:
1 <! -- Internationalization configuration --> 2 <bean id = "localeResolver" class = "org. springframework. web. servlet. i18n. cookieLocaleResolver "/> 3 <bean id =" messageSource "class =" org. springframework. context. support. reloadableResourceBundleMessageSource "> 4 <property name =" basenames "> 5 <list> 6 <value> classpath: resource/ValidationMessages </value> 7 </list> 8 </property> 9 <property name = "useCodeAsDefaultMessage" value = "true"/> 10 </ Bean> 11 12 13 <! -- Register the validators --> 14 <mvc: annotation-driven validator = "validator"/> 15 16 <bean id = "validator" class = "org. springframework. validation. beanvalidation. localValidatorFactoryBean "> 17 <property name =" providerClass "value =" org. hibernate. validator. hibernateValidator "/> 18 <! -- Here the configuration will use the messageSource --> 19 <property name = "validationMessageSource" ref = "messageSource"/> 20 </bean>
Configuration error message: (of course, you can also write it directly in javabean)
1 val.age.message=\u5E74\u9F84\u4E0D\u80FD\u8D85\u8FC720\u5C812 3 username.not.null=\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A4 pwd.not.null=\u5BC6\u7801\u4E0D\u80FD\u4E3A\u7A7A5 username.length=\u7528\u6237\u540D\u6700\u5927\u4E0D\u80FD\u8D85\u8FC7{max},\u6700\u5C0F\u4E0D\u80FD\u5C11\u4E8E{min}6 7 email.format.error=\u90AE\u7BB1\u683C\u5F0F\u4E0D\u6B63\u786E8 9 title.not.null=\u63A8\u9001\u4E3B\u9898\u4E0D\u80FD\u4E3A\u7A7A
Entity (setter getter omitted ):
1 public class ValBean {2 3 4/** 5 * the built-in constraint 6 * @ Null annotation element in Bean Validation must be null 7 * @ NotNull annotation element must not null 8 * @ AssertTrue the annotated element must be true 9 * @ AssertFalse the annotated element must be false 10 * @ Min (value) the element to be commented must be a number and its value must be greater than or equal to the specified minimum value. 11 * @ Max (value) the element to be commented must be a number, the value must be less than or equal to the specified maximum value 12 * @ DecimalMin (value) The annotated element must be a number and its value must be greater than or equal to the specified minimum value 13 * @ DecimalMax (value) the annotated element must be a number and its value must be less than or equal to the specified maximum value. 14 * @ Size (max =, Min =) The size of the element to be commented must be within the specified range. 15 * @ Digits (integer, fraction) the element to be commented must be a number, the value must be within the acceptable range 16 * @ Past the annotated element must be a previous date 17 * @ Future the annotated element must be a Future date 18 *@ pattern (regex =, flag =) The annotated element must comply with the specified Regular Expression 19 * Hibernate Validator appended constraint 20 * @ NotBlank (message =) to verify that the string is not null, and the Length must be greater than 0 21 * @ Email the annotated element must be the Email address 22 * @ Length (min =, max =) the size of the commented string must be within the specified Range 23 * @ NotEmpty the commented string must be non-empty 24 * @ Range (min =, max =, Message =) The annotated element must be within the appropriate range of 25 */26 private Long id; 27 28 @ Max (value = 20, message = "{val. age. message} ") 29 private Integer age; 30 31 @ NotBlank (message =" {username. not. null} ") 32 @ Length (max = 6, min = 3, message =" {username. length} ") 33 private String username; 34 35 @ NotBlank (message =" {pwd. not. null} ") 36 @ Pattern (regexp ="/^ (?! [0-9] + $ )(?! [A-zA-Z] + $) [0-9A-Za-z] {6, 10} $/", message =" the password must be 6 ~ A combination of 10 digits and letters ") 37 private String password; 38 39 40 @ Pattern (regexp =" ^ (13 [0-9]) | (15 [^ 4, \ D]) | (18 [-9]) \ d {8} $ ", message =" Incorrect Mobile phone Number Format ") 41 private String phone; 42 43 @ Email (message = "{email. format. error} ") 44 private String email; 45}
Controller reference:
1 @ Controller 2 @ RequestMapping (value = "/val") 3 public class ValidateController {4 5 @ RequestMapping (value = "/val", method = RequestMethod. POST) 6 @ ResponseBody 7 public LeeJSONResult val (@ Valid @ RequestBody ValBean, BindingResult result) throws Exception {8 9 if (result. hasErrors () {10 // if it fails, the jump prompt is 11 Map <String, String> map = getErrors (result); 12 return LeeJSONResult. error (map); 13} else {14 // continue business logic 15} 16 17 return LeeJSONResult. OK (); 18} 19 20 private Map <String, String> getErrors (BindingResult result) {21 Map <String, String> map = new HashMap <String, String> (); 22 List <FieldError> list = result. getFieldErrors (); 23 for (FieldError error: list) {24 System. out. println ("error. getField (): "+ error. getField (); 25 System. out. println ("error. getdefamessage message (): "+ error. getdefamessage message (); 26 27 map. put (error. getField (), error. getdefamessage message (); 28} 29 return map; 30} 31 32}
Done