Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 3 validation not working

I have a user entity in my application which I need to validate.

public class User {

private String userName;
private String password;

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

public getUserName(){
return this.userName;
}   

// and so on 

}

For this I have created a UsersValidator like below.

public class UserValidator implements Validator {

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

public void validate(Object obj, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName", "field.required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "field.required");
}
}

and I have a controller like this

@RequestMapping(value = "/login", method = RequestMethod.POST)
public String home(@Valid User user,
        BindingResult result) {

    if (result.hasErrors()) {
        return "loginForm";
    } else {
    //continue
}

}

The binding result does not have any errors.

What else I need to do in order for the validation to work? Do I have make any changes in the controller or the spring configuration file.

<mvc:annotation-driven />

<context:component-scan base-package="com.myapp" />

<mvc:resources location="/resources/" mapping="/resources/**" />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass">
        <value>org.springframework.web.servlet.view.tiles2.TilesView</value>
    </property>
</bean>

<bean id="tilesConfigurer"
    class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/tiles.xml</value>
        </list>
    </property>
</bean>

<bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>com/dimex/resourceBundles/ApplicationResources</value>
            <value>com/dimex/resourceBundles/errors</value>
        </list>
    </property>            
  </bean>

<mvc:interceptors>  
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="locale"></property>
    </bean>
</mvc:interceptors>

<bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <property name="defaultLocale" value="en" />
</bean>

EDIT:-

Do I need to have hibernate validator in my classpath. We are not using hibernate in our application. Please help.

EDIT2:-

When I use validation annotations (@NotNull, @Size etc) directly in my entity class then @Valid annotations in controller works but if I remove them from my entities and try to use the validator written above then @Valid does not work.

Is it like that @Valid annotations only work with the validation annotation in the entities only and not with the validators? In order to use my validators will I have to invoke the validate method in my validator directly?

like image 332
ashishjmeshram Avatar asked Jun 03 '11 13:06

ashishjmeshram


1 Answers

You need to put @Component on the Validator implementation so that Spring container can recognize it as:

@Component
public class UserValidator implements Validator {

and use following method in the controller:

@InitBinder(UserVO) protected void initBinder(WebDataBinder binder) { binder.setValidator(userValidator); }

like image 119
user3591738 Avatar answered Oct 13 '22 14:10

user3591738