上篇講到數(shù)據(jù)綁定的數(shù)據(jù)轉(zhuǎn)化店煞,接下來我們開一下數(shù)據(jù)校驗争群,這邊主要講一下JSR-303整合到SpringMVC中型宙。
pom.xml
<properties>
<hibernate-validator.version>5.2.4.Final</hibernate-validator.version>
<validation-api.version>1.1.0.Final</validation-api.version>
</properties>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>${validation-api.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator.version}</version>
</dependency>
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 校驗器注入到處理器適配器中 -->
<mvc:annotation-driven validator="validator"/>
<context:component-scan base-package="validator"/>
<!-- 國際化的消息資源文件(本系統(tǒng)中主要用于顯示/錯誤消息定制) -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<!-- 在web環(huán)境中一定要定位到classpath 否則默認到當前web應(yīng)用下找 -->
<value>classpath:messages</value>
</list>
</property>
<property name="useCodeAsDefaultMessage" value="false"/>
<property name="defaultEncoding" value="UTF-8"/>
<property name="cacheSeconds" value="60"/>
</bean>
<!-- 以下 validator ConversionService 在使用 mvc:annotation-driven 會自動注冊-->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<!-- hibernate校驗器-->
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
<!-- 如果不加默認到 使用classpath下的 ValidationMessages.properties -->
<property name="validationMessageSource" ref="messageSource"/>
</bean>
<!-- ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
messages.properties
user.id.null=用戶編號不能為空
user.name.null=用戶名不能為空
user.name.length.illegal=用戶名長度必須在5到20之間
user.name.illegal=用戶名必須是字母
user.password.null=密碼不能為空
核心類
@Data
public class User implements Serializable {
//使用自定義錯誤信息
@NotNull(message = "{user.id.null}")
private Long id;
@NotEmpty(message = "{user.name.null}")
@Length(min = 5, max = 20, message = "{user.name.length.illegal}")
@Pattern(regexp = "[a-zA-Z]{5,20}", message = "{user.name.illegal}")
private String name;
@NotNull(message = "{user.password.null}")
private String password;
}
@Controller
public class UserController {
@RequestMapping("/save")
//BindingResult講保存錯誤信息妓湘,到時候自己根據(jù)業(yè)務(wù)處理
public String save(@Valid User user, BindingResult result) {
if (result.hasErrors()) {
return "error";
}
return "success";
}
}