Java Bean Validation (JSR 303) defines a meta-data-model and an API for validation of classes. Implementations of this specification makes it possible to define and perform validation using annotations or XML. The example below illustrates this for a username: @ NotNull american tactical imports @ Size (min = 5, max = 20) @ Pattern (regexp american tactical imports = "[a-zA-Z0-9 \ \. _-] +") Private String username;
Bean Validation is not tied to any particular framework or stock. However, there is good support for Bean Validation in most popular frameworks american tactical imports such as Hibernate and JSF second Moreover, it is easy to perform validation via API: import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; ... ValidatorFactory Factory = Validation.buildDefaultValidatorFactory (); Validator validator = factory.getValidator (); Set <ConstraintViolation american tactical imports <CUSTOMER>> constraintViolations = validator.validate (customer);
To get started with Bean Validation (Hibernate Validator 4.x is the reference implementation) in a Maven project add the following dependencies in your pom: <dependency> american tactical imports <groupId> org.hibernate american tactical imports </ GroupID> <artifactId> hibernate-validator < / artifactId> <version> 4.2.0.Final </ version> </ dependency> <dependency> <groupId> org.slf4j american tactical imports </ GroupID> <artifactId> slf4j-jdk14 </ artifactId> <version> 1.6.1 </ version> </ dependency>
Use Hibernate and JSF 2 is usually sufficient to add the above dependencies american tactical imports as long as they are available in the application's class path. Used JSF 2 can, for example, to enable validation for an input field as follows: <h:inputText value="#{customer.age}"> <f:validateBean/> </ h: input text> american tactical imports
Email info@cygni.se
Other Site About Cookies
No comments:
Post a Comment