Java Reference
In-Depth Information
Chapter 6
Validating Data
The Bean Validation API is utilized for validating data values in both Java SE and EE applications. As of Java EE 6,
Bean Validation was included in the platform, adding integrated validation capabilities for enterprise applications.
JavaServer Faces applications can utilize bean validation for validating values that are entered on an input form or
even for calculations that are processed on the server. The best part is that the validation can occur with minimal
programming because annotations are utilized to declaratively apply constraints against data for validation.
The Bean Validation 1.1 API is included in the Java EE 7 platform, and it brings forth a handful of welcome
enhancements, as well as some new features that make validation even more useful. This chapter covers those new
features and provides brief overviews of how they are to be used.
An Overview of Bean Validation with Java EE
The Bean Validation API can be used with many different technologies in both SE and EE environments. In the
context of Java EE, Bean Validation can be performed by placing annotations on a field, method, or class such as a
JSF managed bean or entity class. For instance, in a Java EE application that utilizes the JSF framework, validation
occurs when a user enters values into a view and those values are propagated to the server via either a form submit
or Ajax. To validate a specified entity field, simply annotate that field within the managed bean with the appropriate
annotation according to the validations that you want to apply. When the JSF Process Validations phase occurs,
the value that was entered by the user will be validated based upon the specified validation criteria. At this point,
if the validation fails, the Render Response phase is executed, and an appropriate error message is added to the
FacesContext . However, if the validation is successful, then the life cycle continues normally.
Suppose you wanted to perform validation on the fields of an entity class, such that the values for each field will
be validated accordingly when a user has entered the values for creating a new entity object. In the following example,
the AuthorWork entity has been enhanced to include bean validation for the id , address1 , state , and zip fields,
by incorporating some of the built-in constraint validation annotations:
...
@Entity
@Table(name = "AUTHOR_DETAIL")
public class AuthorDetailBeanValidation implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@SequenceGenerator(name="author_detail_s_generator",sequenceName="author__detail_s",
initialValue=1, allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE,
generator="author_detail_s_generator")
@NotNull
 
Search WWH ::




Custom Search