Core features of javaee6-bean verification framework (organization)

Source: Internet
Author: User

Core features of javaee6-bean verification framework (organization)

 

Overview

 

Before Bean Validation of Java EE 6 appeared, developers had to write verification rules in the presentation layer framework, business layer, and persistence layer to ensure the synchronization of these rules, however, this is a waste of time and error-prone. Bean Validation is implemented by constraints. These constraints appear in the form of annotations. Annotations can be placed on attributes, methods, or classes of JavaBean (such as backing bean. The constraint can be either a built-in annotation (located under the javax. validation. constraints package) or user-defined.

 

 

JSR303 built-in constraint rules

 

@ AssertTrue/@ AssertFalse applicable field for verification: boolean annotation Description: whether the verification value is true/false attribute description:-@ DecimalMax/@ DecimalMin applicable field for verification: BigDecimal, BigInteger, String, byte, short, int, long annotation Description: whether the verification value is smaller than or equal to the specified small value, pay attention to the precision problem of the decimal attribute description: Public @ Digits verification applicable field: BigDecimal, bigInteger, String, byte, short, int, long description: whether the number of the verification value constitutes a valid attribute description: integer: specifies the number of digits in the integer part. Fraction: specifies the number of digits in the decimal part. @ Future/@ Past verification applicable field: Date, Calendar annotation Description: whether the verification value is after the current time/before the attribute description: Public @ Max/@ Min verification applicable field: BigDecimal, bigInteger, String, byte, short, int, long annotation Description: whether the verification value is smaller than or equal to the specified integer attribute description: Public @ NotNull/@ Null verification applicable field: reference data type description: whether the verification value is non-empty/empty attribute description: Common @ Pattern verification applicable field: String annotation Description: whether the verification value is equipped with a regular expression attribute description: regexp: regular Expression flags: Specifies Pattern. flag array, indicating the related options of the regular expression. @ Size verification applicable fields: String, Collection, Map, array annotation Description: whether the verification value meets the length requirements attribute description: max: specifies the maximum length, min: specifies the minimum length. @ Valid verification applicable field: Reference Type annotation Description: whether the verification value requires recursive verification attribute description: None


 

Instance:

 

@ Entity @ Table (name = agent) @ SequenceGenerator (name = seq_gen, sequenceName = seq_agent) public class AgentEntity extends IdEntity {@ NotBlank (message = name cannot be blank) @ Size (max = 128, message = Name length exceeds 128) @ Pattern (regexp = ^ [\ u4e00-\ u9fa5 \ w \ d _] * $, message = name not compliant) private String name; @ NotBlank (message = Code cannot be blank) @ Size (max = 16, message = code length exceeds 16) @ Pattern (regexp = ^ [0-9a-zA-Z _] * $, message = code does not comply with the specifications) private String code; private AgentStatusEnum status; private String description; private Long createTime; private String createUser; private Long updateTime; private String updateUser; private Long lastHeartBeat; public AgentEntity () {super (); this. status = AgentStatusEnum. OFFLINE;} @ Column (name = name) public String getName () {return name;} public void setName (String name) {this. name = name ;}@ Column (name = code) public String getCode () {return code;} public void setCode (String code) {this. code = code ;}@ Enumerated (EnumType. STRING) public AgentStatusEnum getStatus () {return status;} public void setStatus (AgentStatusEnum status) {this. status = status;} public String getDescription () {return description;} public void setDescription (String description) {this. description = description;} @ Column (name = create_time) public Long getCreateTime () {return createTime;} public void setCreateTime (Long createTime) {this. createTime = createTime;} @ Column (name = create_user) public String getCreateUser () {return createUser;} public void setCreateUser (String createUser) {this. createUser = createUser;} @ Column (name = update_time) public Long getUpdateTime () {return updateTime;} public void setUpdateTime (Long updateTime) {this. updateTime = updateTime;} @ Column (name = update_user) public String getUpdateUser () {return updateUser;} public void setUpdateUser (String updateUser) {this. updateUser = updateUser;} @ Column (name = last_heart_beat) public Long getLastHeartBeat () {return lastHeartBeat;} public void setLastHeartBeat (Long lastHeartBeat) {this. lastHeartBeat = lastHeartBeat ;}}

 

Bytes -------------------------------------------------------------------------------------------------------------------

Supplement: validate api

The following content is excerpted from: http://developer.51cto.com/art/201003/190130.htm

 

 

Validation API


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

ConstraintValidator: This is an interface, which must be implemented by the Constraints Verification class. This interface defines the relevant logic 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. The implementation of this interface must be thread-safe.


ConstraintViolation: The ConstraintViolation interface indicates that the constraint verification on the given bean fails. It discloses the context of the constraint violation and information describing the violation.


ValidationException: if some unrecoverable errors occur during verification, a ValidationException is thrown. In some cases, this exception can be specified, such as the definition of an invalid group, the definition of an invalid constraint, and the declaration of an invalid constraint.


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. The Bean Validation specification aims to provide a verification engine and metadata repository for object constraints. The Framework (Java EE or Java SE) that requires constraint definition, verification, and metadata can use the Bean Validation specification to implement 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 and Bean Validation can determine which property to bind and perform related verification. They will also display information about the violation of the constraint to the user. Hibernate Validator 4 is a reference implementation framework of Bean Validation specifications. The latest version of Hibernate Validator 4 has many new features, such as grouping verification, natural integration with JPA 2 and JSF 2, and extended annotation sets.

Related Article

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.