Java – invalid spring 3 validation

There is a user entity in my application that needs to be verified

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

}

To do this, I created a usersvalidator, as shown 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");
}
}

I have such a controller

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

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

}

There are no errors in the binding results

What else needs to be done to verify the work? I made any changes in the controller or 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:-

I need to use the hibernate validator in my classpath We don't use hibernate in our application Please help.

EDIT2: –

When I use validation annotations (@ notnull, @ size, etc.) directly in the entity class, @ valid annotations in the controller work, but if I delete them from my entity and try to use the validator written above, @ valid does not work

So @ valid annotation can only be used in entities, not with validators? In order to use my validator, I must call the validate method directly in my validator.

Solution

From what I understand, you are trying to combine JSR - 303 @ valid annotation with classic spring validation Why do you want to do this? The equivalent jsr-303 comment for the uservalidator class will be as follows:

@NotNull
@Size(min=1)
private String userName;

@NotNull
@Size(min=1)
private String password

...

Spring documentation describes the steps required to configure JSR - 303 validation You need to use hibernate validator (or another JSR - 303 provider) to make the above functions work properly You also need to configure the validator bean as follows

<bean id="validator"
      class="org.springframework.validation.beanvalidation.LocalValidatorfactorybean"/>

The other answers to this question are the same

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>