Integrate ZK with JSR 303: Bean Validation

From Documentation
Revision as of 03:07, 13 May 2011 by SimonPai (talk | contribs)
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