相關文章:
一、簡介
Spring Framework 4.0支持Bean Validation 1.0 (JSR-303) 和 Bean Validation 1.1 (JSR-349),也適配Spring的Validator
接口。
關于把校驗作為業務邏輯現在有許多不同的觀點,Spring為校驗(和數據綁定)提供了一種設計既不贊成也不反對那些觀點。明確的校驗不應該被綁在web層,應該是在容易發現的位置盡可能的插入任何可用的校驗器??紤]以上的觀點,Spring想了一個Validator
接口,可以普遍或者特定使用在應用程序的每個層面。
數據綁定會將用戶輸入動態的綁定到一個域模型(或者任何你用來處理用戶輸入的對象)。Spring提供了DataBinder
來準確的執行數據綁定。Validator
和DataBinder
一起組成了validation
包,它主要是用在MVC framework,但是并不局限于MVC。
二、Spring校驗
Spring 3引進了許多增強到它的校驗支持。首先JSR-303 Bean Validation API現在是完全支持的。Spring的DataBinder現在可以校驗數據的同時綁定它們。Spring MVC支持聲明式校驗@Controller
輸入。
2.1 JSR-303 Bean Validation API概覽
JSR-303是為Java平臺制定的約束聲明和元數據標準化規范。你可以在域模型(實體類)的屬性上使用注解式聲明校驗約束條件。你也可以定義自己的約束條件。
為了方便解釋,我看來看一個PersonForm
模型:
public class PersonForm {
private String name;
private int age;
}
JSR-303允許你在這些屬性前定義約束條件:
public class PersonForm {
@NotNull
@Size(max=64)
private String name;
@Min(0)
private int age;
}
當這個類的實例被一個JSR-303校驗器校驗時,這些約束條件將會被強制執行。
關于JSR-303/JSR-349的更多信息參考Bean Validation website
2.2 配置Bean Validation Provider
Spring完全支持Bean Validation API。包括方便的將JSR-303/JSR-349 Bean Validation provider作為一個Spring bean來使用。這允許你可以在任何時間將javax.validation.ValidatorFactory
或者javax.validation.Validator
注入到應用程序需要被驗證的地方。
使用LocalValidatorFactoryBean
來配置一個默認的校驗器作為一個Spring bean:
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
LocalValidatorFactoryBean
同時實現了javax.validation.ValidatorFactory
、javax.validation.Validator
和org.springframework.validation.Validator
。你可以注入它們其中任何一個引用到需要執行校驗邏輯的bean。
下面這個例子注入了一個javax.validation.Validator
的引用:
import javax.validation.Validator;
@Service
public class MyService {
@Autowired
private Validator validator;
2.3 Spring MVC 3 Validation
從Spring 3開始,Spring MVC可以自動校驗@Controller
的輸入。
2.3.1 觸發@Controller輸入校驗
為了觸發校驗,只需簡單的在輸入參數前添加@Valid
注解:
@Controller
public class MyController {
@RequestMapping("/foo", method=RequestMethod.POST)
public void processFoo(@Valid Foo foo) { /* ... */ }
Spring MVC在數據綁定結束后會使用一個合適的校驗器來進行校驗。
注意:@Valid
注解是JSR-303 Bean Validation API的一部分,并不是Spring特有的結構。
你可以配置一個自定義的校驗器或者使用JSR-303/JSR-349的校驗器。
下面的例子使用自定義校驗器:
@Controller
public class MyController {
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new FooValidator());
}
@RequestMapping("/foo", method=RequestMethod.POST)
public void processFoo(@Valid Foo foo,BindingResult bindingResult) { ... }
}
還可以配置一個全局的自定義校驗器:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<mvc:annotation-driven validator="globalValidator"/>
</beans>
怎么實現自己的校驗器可以看這里。
第二種方式是使用JSR-303/JSR-349的校驗器,例如:Hibernate Validator。將他添加到你的classpath,Spring MVC會自動發現。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<!-- JSR-303/JSR-349 support will be detected on classpath and enabled automatically -->
<mvc:annotation-driven/>
</beans>
驗證的錯誤可以通過BindingResult
的getFieldError()
來獲取字段的錯誤。
后續會補充下JSR-303的一些注解。