Integrate ZK with JSR 303: Bean Validation"

From Documentation
m
Line 8: Line 8:
  
 
==What is JSR 303: Bean Validation==
 
==What is JSR 303: Bean Validation==
 +
JSR 303 is a standard designed for working on property validation in Java beans. In brief, the constraints of a bean property are specified by Java annotations, and validation against the bean can be invoked by API.
  
 +
See also: [http://people.redhat.com/~ebernard/validation/ JSR 303 Documentation]
 +
 +
 +
 
 
==How to use Bean Validation==
 
==How to use Bean Validation==
 +
The usage of this tool is quite straightforward: You just need to specify property constraints and invoke validation.
 +
 +
===Bean===
 +
 +
<source lang="Java">
 +
public class User {
 +
 +
@NotEmpty(message = "Last name can not be null.")
 +
private String _lastName;
 +
 +
@Past(message = "Birth date must be in the past.")
 +
private Date _birthDate;
 +
 +
}
 +
</source>
 +
 +
===Invoke Validation===
 +
 +
<source lang="Java">
 +
// prepare for Validator
 +
ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
 +
Validator validator = vf.getValidator();
 +
 +
// suppose we have a bean of User like this:
 +
User user = new User();
 +
user.setLastName(""); // wrong, should not be empty
 +
 +
// invoke validation, which will return a set of violations to the constraints
 +
Set<ConstraintViolation<User>> violations = validator.validate(user);
 +
for(ConstraintViolation<User> v : violations)
 +
System.out.println(v.getMessage()); // will print out: "Last name can not be null."
 +
</source>
 +
  
 +
&nbsp;
 
==How to use Bean Validation in your ZK application==
 
==How to use Bean Validation in your ZK application==
  
 +
 +
&nbsp;
 +
===Jar Dependencies===
 +
 +
 +
&nbsp;
 +
===Setup===
 +
 +
 +
&nbsp;
 
==Demo==
 
==Demo==
  

Revision as of 03:07, 13 May 2011

DocumentationSmall Talks2011MayIntegrate ZK with JSR 303: Bean Validation
Integrate ZK with JSR 303: Bean Validation

Author
Simon Pai, Engineer, Potix Corporation
Date
May 20, 2011
Version
ZK 5.0.7

WarningTriangle-32x32.png This page is under construction, so we cannot guarantee the accuracy of the content!

What is JSR 303: Bean Validation

JSR 303 is a standard designed for working on property validation in Java beans. In brief, the constraints of a bean property are specified by Java annotations, and validation against the bean can be invoked by API.

See also: JSR 303 Documentation


 

How to use Bean Validation

The usage of this tool is quite straightforward: You just need to specify property constraints and invoke validation.

Bean

public class User {
	
	@NotEmpty(message = "Last name can not be null.")
	private String _lastName;
	
	@Past(message = "Birth date must be in the past.")
	private Date _birthDate;
	
}

Invoke Validation

// prepare for Validator
ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
Validator validator = vf.getValidator();

// suppose we have a bean of User like this:
User user = new User();
user.setLastName(""); // wrong, should not be empty

// invoke validation, which will return a set of violations to the constraints
Set<ConstraintViolation<User>> violations = validator.validate(user);
for(ConstraintViolation<User> v : violations)
	System.out.println(v.getMessage()); // will print out: "Last name can not be null."


 

How to use Bean Validation in your ZK application

 

Jar Dependencies

 

Setup

 

Demo