开发者

spring 3 validation example

开发者 https://www.devze.com 2023-02-20 00:00 出处:网络
Can anybody give me a link for Spring 3.0 Validation, by extending org.springframework.validation.Validator or javax.validation Interface and reading error messages from properties file?

Can anybody give me a link for Spring 3.0 Validation, by extending org.springframework.validation.Validator or javax.validation Interface and reading error messages from properties file?

Thanks开发者_开发技巧

Shams


This 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");
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消