Java ee6 core features: bean Validation Analysis

Source: Internet
Author: User

Bean validation (JSR 303) -- Java
EE
A core feature of 6, which defines a metadata model and API for entity verification. The default metadata source is annotation, but developers can extend it through the XML descriptor.
The validation API does not rely on a specific application layer or programming model, so that the same set of verification can be shared by all layers of the application. It also provides
API to increase the mechanism of custom verification constraints and the means to query the restricted metadata warehouse.

Bean in jee6
Before the emergence of validation, developers had to write verification rules in the presentation layer framework, business layer, and persistence layer to ensure the synchronization of these Rules, but this was a waste of time and error-prone.
Bean validation is implemented by constraints. These constraints appear in the form of annotations, which can be placed in a JavaBean (such as backing
Bean.
The constraint can be either a built-in annotation (located under the javax. validation. Constraints package) or user-defined. Some common built-in annotations are listed as follows:

  • Min: the element annotated by @ Min must be a number whose value must be greater than or equal to the given minimum value.
  • MAX: the element annotated by @ Max must be a number whose value is smaller than or equal to the given maximum value.
  • Size: @ size indicates that the annotated element must be between the given minimum and maximum values. Data Types that support size verification include string,
    Collection, map, and array.
  • Notnull: @ notnull ensures that the element to be annotated cannot be null.
  • Null: @ null ensure that the element to be annotated must be null.
  • Pattern: @ pattern ensure that the annotated element (string) matches the given Java
    Zheng
    The expression.

In the following sample code, some constraints are declared using bean validation Annotations:

Java
Code
  1. Public
     
    Class
    Address {
  2. @ Notnull
     
    @ Size
    (Max =
    30
    )
  3. Private
    String addressline1;
  4. @ Size
    (Max =
    30
    )
  5. Private
    String addressline2;
  6. Public
    String getaddressline1 (){
  7. Return
    Addressline1;
  8. }
  9. Public
     
    Void
    Setaddressline1 (string addressline1 ){
  10. This
    . Addressline1 = addressline1;
  11. }
  12. }
public class Address {       @NotNull @Size(max=30)       private String addressline1;       @Size(max=30)       private String addressline2;       public String getAddressline1() {          return addressline1;       }       public void setAddressline1(String addressline1) {          this.addressline1 = addressline1;       }}

@ Notnull: the element addressline1 to be annotated cannot be null; @ size: the element addressline1 and
Addressline2 cannot exceed the given maximum value, that is, 30 characters.

When verifying the address object, the value of addressline1 is passed to the verification class for the @ notnull constraint and the validation class for the @ size constraint.
The value of addressline2 is passed to the verification class for the @ size constraint and verified by the related verification class.

The following code defines a constraint named zipcode:

Java
Code
  1. @ Size
    (Min =
    5
    , Max =
    5
    )
  2. @ Constraintvalidator
    (Zipcodevalidator.
    Class
    )
  3. @ Brief ented

  4. @ Target
    ({Annotation_type, method, field })
  5. @ Retention
    (Runtime)
  6. Public
     
    @ Interface
    Zipcode {
  7. String message () Default
     
    "Wrong zipcode"
    ;
  8. String [] groups () Default
    {};
  9. }
@Size(min=5, max=5)   @ConstraintValidator(ZipcodeValidator.class)   @Documented   @Target({ANNOTATION_TYPE, METHOD, FIELD})   @Retention(RUNTIME)   public @interface ZipCode {       String message() default "Wrong zipcode";       String[] groups() default {};}

You can use @ zipcode on classes, attributes, or methods, just like other constraints.

Java
Code
  1. Public
     
    Class
    Address {
  2. @ Zipcode

  3. Private
    String zipcode;
  4. Public
    String getzipcode (){
  5. Return
    Zipcode;
  6. }
  7. Public
     
    Void
    Setzipcode (string zipcode ){
  8. This
    . Zipcode = zipcode;
  9. }
  10. }
public class Address {       @ZipCode       private String zipCode;       public String getZipCode() {          return zipCode;       }       public void setZipCode(String zipCode) {          this.zipCode = zipCode;       }}

Validation API

Developers can verify JavaBean programmatically by using the validation API. Bean Validation
The default API package is javax. validation. The following describes some classes in the package:

Constraintvalidator
: This is an interface. The specific constraints verification class must implement this interface. This interface defines related
To verify the constraints in the given object type.

Validator
: Validahttp: // Java
.Sun.com/javaee
/6/docs/API/index.html? The javax/validation/validator.html tor interface holds the contract of the object verification diagram. This connection
The implementation of the port must be thread-safe.

Constraintviolation
: Constraintviolation interface indicates
Failed to verify the constraint. It discloses the context of the constraint violation and information about the violation.

Validationexception
: If some unrecoverable errors occur during verification
Validationexception exception. In some cases, this exception can be specified, such as illegal group definitions, invalid constraint definitions, and invalid constraint declarations.
.

Constraint metadata request API

The bean validation specification provides a means to query the restricted warehouse. This API is mainly used for tool support and integration with other frameworks, libraries, and JSR. Bean
The purpose of the validation specification is to provide a verification engine and metadata repository for object constraints. The framework that requires constraint definition, verification, and metadata (Java
EE or Java

Se) bean validation specifications can be used to accomplish these functions. From the perspective of applications or infrastructure, this can avoid unnecessary repetitive work.

Bean validation has been integrated into JSF 2.0 and JPA 2.0. In JSF, You can bind the form input field to the attribute of the Field object. JSF
2. Bean validation can determine which property to bind and perform relevant verification. It also displays information about the violation of the constraint to the user.

Hibernate validator 4 is Bean
The reference implementation framework of the validation specification. The latest version has many new features, such as grouping verification, natural integration with JPA 2 and JSF 2, and extended annotation sets.

 

From: http://www.javaeye.com/news/15265

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.