Searching...
Saturday, 2 January 2016

Spring Validation Framework Example

20:59
Spring provides an api to validate data using its validation framework. There is an interface org.springframework.validation.Validator will handles the data validation. Here is one simple example to validate user details.
1. Create a User bean
package com.vinod.spring;

import org.springframework.stereotype.Component;

@Component
public class User {
private String userName;
private String password;

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

}
 
1. Create a Validator class
package com.vinod.spring;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

@Component
public class UserValidator implements Validator {

public boolean supports(Class clazz) {
return User.class.equals(clazz);
}

public void validate(Object target, Errors errors) {
User user = (User) target;

ValidationUtils.rejectIfEmpty(errors, "userName", "UserNamerequired",
"UserName should not be blank");
ValidationUtils.rejectIfEmpty(errors, "password", "PasswordRequired",
"password should not be blank");
if (user.getUserName() != null && user.getUserName().length() < 4) {
errors.rejectValue("UserName", "errormsg.UserName",
"UserName should not be less than 4 characteros");
}
if (user.getPassword() != null && user.getPassword().length() < 4) {
errors.rejectValue("password", "errormsg.password",
"Password should not be less then 4 characters");
}
}

}

3. Spring Configuration

<beans xmlns:context="http://www.springframework.org/schema/context" 
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="
    http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">

<context:component-scan base-package="com.vinod">
<task:annotation-driven>
</task:annotation-driven></context:component-scan></beans>

4. Main class

package com.vinod.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.validation.ValidationUtils;

/**
 *@authorvinod.kumaran
 *
 */
public class UserValidatorTest {

public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext(
"spring-context.xml");
User user = (User) context.getBean(User.class);
user.setUserName("Vin");
user.setPassword("Pas");
UserValidator userValidator = (UserValidator) context
.getBean(UserValidator.class);
BindException errors = new BindException(user, User.class.getName());

ValidationUtils.invokeValidator(userValidator, user, errors);

if (errors.hasErrors()) {
System.out.println("Errors: " + errors.getErrorCount());
for (ObjectError objectErrors : errors.getAllErrors()) {
System.out.println(objectErrors.getDefaultMessage());
}
}

}

}

5. Output

Errors: 2
UserName should not be less than 4 characteros

Password should not be less then 4 characters

0 comments: