JSR 303: Bean Validation)
implemented in Java EE 6. The standard validation framework (Java EE 6 標準規範下 Bean 的驗證框架與實作. 發展源由
JSR-303JSR 303 是 JAVA EE 6 中的一項子規範 - Bean Validation,官方參考實作是 Hibernate Validator,此實作與 Hibernate ORM 沒有任何關係。JSR 303 用於對 Java Bean 中的欄位值進行驗證。 Bean Validation 是通過約束實作的,這些約束以 Annotation 的形式出現,註釋型別可以放在 JavaBean(如 ManagedBean)的屬性、方法或是 Class 上面。 約束既可以是內建的註釋型別(位於 javax.validation.constraints 套件包底下),也可以由使用者自定義。 Bean Validation 已經整合到 JSF 2.0 和 JPA 2.0 中。在 JSF 中可以將表單輸入域值與域值物件的屬性綁定起來。JSF 2 和 Bean Validation 可以判斷出綁定的是哪個屬性並執行與之相關的驗證,還會將約束違背的訊息顯示給使用者。 使用Validator的好處.使用Validator框架比一般的在應用程序的代碼中定義驗證規則有好多優點,如:
面你將會看到使用Validator框架內置的驗 證規則來更好地完成你的工作,而更重要的是,Validator框架允許你自定義驗證程序,並插入到框架中。 Java EE 6 Bean Validation 提供了實體驗證元資料模型與API 作者 Srini Penchikala 參考實作(RI)Hibernate Validator 4http://hibernate.org/subprojects/validator.htmlHibernate Validator 4.x is the reference implementation for JSR 303 - Bean Validation. JSR 303 defines a metadata model and API for JavaBean validation. The default metadata source is annotations, with the ability to override and extend the meta-data through the use of XML validation descriptors. The API is not tied to a specific application tier or programming model. It is specifically not tied to either the web tier or the persistence tier, and is available for both server-side application programming, as well as rich client Swing application developer. Bean Validator(JSR-303) 規範的標準實作,此版本包含了許多新特性: (較標準多出許多特性!)
Apache Bean-Validator Projecthttp://incubator.apache.org/bval/cwiki/index.htmlhttp://code.google.com/p/agimatec-validation/ The goal of the Bean Validation project is to deliver an implementation of the Bean Validation Specfication (JSR303), which is TCK compliant and works on Java SE 5 or later. The initial codebase for the project was donated to the ASF by a SGA from Agimatec GmbH and uses the ASL 2.0 license. First Exampleimport java.util.Set; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; /** * * @author WisdomFish, Kuo */ public class Person { @NotNull @Min(value = 1) @Max(value = 9999) private Long id; @NotNull @Size(min = 1, max = 20, message = "姓名的長度必須在{min}和{max}之間!") private String name; static ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); static Validator validator = vf.getValidator(); public static void main(String[] args) { Person s = new Person(); s.name = "123456789012345678901"; Set<ConstraintViolation<Person>> set = validator.validate(s); for (ConstraintViolation<Person> constraintViolation : set) { System.out.println(constraintViolation.getMessage()); } } } Running ... 姓名的長度必須在1和20之間! may not be null
Defining constraintsAPI, http://java.sun.com/javaee/6/docs/api/javax/validation/constraints/package-summary.html Constraints can be built-in or user-defined. Several built-in annotations are available in the javax.validation.constraints package. Some of the commonly used built-in annotations are listed below:
常用的內建註解列舉如下:
http://wiki.redsaga.com/confluence/display/HART/Hibernate+Validator Custom validationValidating constraintsMessage(s)@NotNull(message="必要有值!") @Min(value = 1, message="最小為{value}!") @Max(value = 5, message="最大為{value}!") @Size(min=0, max=1, message="最大最小字元為{min}!") private String name; Ajax validation for JSF inputshttp://forums.java.net/jive/thread.jspa?messageID=381857 http://stackoverflow.com/questions/2329899/jsf-2-with-hibernate-validator-4-and-tomcat-6 References
|