springmvc 整合数据验证框架 jsr
1、maven
<dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.1.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.2.0.Final</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.4</version> </dependency>
2、修改配置文件applicationContex.xml
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/> <!--不设置则默认为classpath下的 ValidationMessages.properties --> <property name="validationMessageSource" ref="validatemessageSource"/> </bean> <bean id="conversion-service" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" /> <bean id="validatemessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="classpath:validatemessages"/> <property name="fileEncodings" value="utf-8"/> <property name="cacheSeconds" value="120"/> </bean>
<mvc:annotation-driven validator="validator" />
validator="validator"是上面定义的id
3、新建validationMessages.properties 文件放到resources文件夹中,内容如下
name.not.empty=1111111
111111内容可自定义
4、新建实体类
package com.db.pojo; import javax.validation.constraints.NotNull; import org.hibernate.validator.constraints.NotBlank; import org.hibernate.validator.constraints.NotEmpty; public class User { @NotEmpty(message="{name.not.empty}") private String userName; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
name.not.empty 为validationMessages.properties里面配置的名称
@NotEmpty代表不能为空,可从网上查询其它验证标签
5、查看controller
@RequestMapping(value="/userlogin", method = {RequestMethod.GET}) public String userLogin(Model model){ if(!model.containsAttribute("contentModel")){ model.addAttribute("contentModel", new User()); } return "userlogin"; } @RequestMapping(value="/userlogin", method = {RequestMethod.POST}) public String userLogin(@Valid @ModelAttribute("contentModel") User user, BindingResult result){ Map<String, Object> map = new HashMap<String, Object>(); if(result.hasErrors()){ return "/userlogin"; } else { return "/index"; } }
6、jsp页面
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <html> <head> <title>Reservation Form</title> <style> .error { color: #ff0000; font-weight: bold; } </style> </head> <body> <form:form method="post" modelAttribute="contentModel" action="userlogin"> <form:errors path="*" cssClass="error" /> <table> <tr> <td>Name</td> <td><form:input path="userName" /> </td> <td><form:errors path="userName" cssClass="error" /> </td> </tr> <tr> <td>password</td> <td><form:input path="password" /> </td> <td><form:errors path="password" cssClass="error" /> </td> </tr> <tr> <td colspan="3"><input type="submit" /> </td> </tr> </table> </form:form> </body> </html>
访问http://****/userlogin 点击submit 按钮会提示 1111111(就是validationMessages.properties文件中配置的内容)