org.springframework.validation.Validator
or javax.validation
Interface and reading error messages from properties file?
Thanks开发者_开发技巧
ShamsThis is a good link to start with Spring 3 validation Spring 3 MVC: Show validation message with custom validator.
for reading properties file use:
ValidationUtils.rejectIfEmpty(errors, "userId", "field.required");
in Validation class. and using
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value = "messages" />
</bean>
in dispatcher-servlet.xml file to load message.properties file.
And it is working fine..
Cheers
Shams
public class LoginValidator implements Validator {
public boolean supports(Class aClass) {
return Login.class.equals(aClass);
}
public void validate(Object obj, Errors errors) {
Login login = (Login) obj;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName",
"username.required", "Required field");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userPassword",
"userpassword.required", "Required field");
}
}
精彩评论