Spring portlet mvc:@Valid似乎不起作用

时间:2010-11-26 08:27:50

标签: java spring validation portlet spring-portlet-mvc

我创建了一个bean类并在我的控制器中使用它,但它似乎不起作用。 即使我输入的年龄无效,result.hasErrors仍然是假的。

Bean类:

public class User{
    @Min(13)
    private int age;
    private String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName(){
            return name;
    }

    public void setName(String name){
            this.name = name;
    }   
}

Controller snippet:

@ActionMapping(params = "myAction=validateUser")
    public void validateUser(ActionRequest request, ActionResponse response, ModelMap model, @ModelAttribute("user") @Valid User user, BindingResult result ){      

        if(result.hasErrors()){
            for(ObjectError oe : result.getAllErrors()){
                System.out.println(oe.getDefaultMessage());
            }
        } else{
            //code
        }
    }

JSP:

<form:form action="${registerUser}" method="post" commandName="user">
    <b>User</b> 
    <form:input path="age"/>
    <form:input path="name"/>
    <input type="submit" value="register"/>
</form:form>

编辑: 我的userRegistration-portlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/mvc
        ">

    <mvc:annotation-driven /> 

    <import resource="spring-hibernate.xml"/>

    <context:component-scan base-package="comjohndoe.dao" />
    <context:component-scan base-package="comjohndoe.model" />
    <context:component-scan base-package="comjohndoe.service" />
    <context:component-scan base-package="comjohndoe.util" />
    <context:component-scan base-package="comjohndoecontroller" />

    <bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

这是mvc:spring-validation系列,它给出了cvc-complex-type.2.4.c the matching wildcard is strict, but no declaration can be found for the element mvc:annotation-driven.错误。

4 个答案:

答案 0 :(得分:8)

我有完全相同的问题。我阅读https://jira.springframework.org/browse/SPR-6817并通过添加到我的配置来解决问题:

<mvc:annotation-driven validator="validator" />

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

<bean id="annotationMethodHandlerAdapter" class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="webBindingInitializer">
        <bean id="configurableWebBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <property name="validator">
                <ref bean="validator"/>
            </property>
        </bean>
    </property>
</bean>

您还需要在项目中安装JSR-303验证器以使其可用为此,我使用了Hibernate Validator:http://www.hibernate.org/subprojects/validator.html

答案 1 :(得分:4)

您需要<mvc:annotation-driven />才能启用jsr-303验证。如果您由于某种原因不想使用它,或者在某些时候它开始产生问题(就像它对我做的那样),请查看this question

更新: 在schemaLocation中,mvc条目应包含这些 two

http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

答案 2 :(得分:2)

您需要执行以下操作,这就是我在spring portlet mvc项目中解决的问题。

  1. mvc:annotation-driven添加到您的applicatonContext.xml

  2. 在控制器中添加以下代码:

    @Autowired private LocalValidatorFactoryBean validator;

    @InitBinder public void initBinder(WebDataBinder binder) { binder.setValidator(validator); }

答案 3 :(得分:0)

我认为原因是ServletRequestDataBinder(扩展DataBinder)没有org.springframework.validation.Validator实例,所以它不验证任何东西(验证@Valid带注释的bean,你必须有javax.validation.Validator实例)

<强> org.springframework.validation.DataBinder

    public void validate() {
            Validator validator = getValidator();
            if (validator != null) {
                validator.validate(getTarget(), getBindingResult());
            }
        }

您可以在@InitBinder带注释的方法中设置Validator实例: -

@InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.setValidator(new MyValidator());
    }

Yo也可以在WebBindingInitializer中设置验证器实例。

StandardBindingInitializer

   public class StandardBindingInitializer implements WebBindingInitializer {
        @Autowired
        private LocalValidatorFactoryBean validator;
        public void setValidatorFactory(LocalValidatorFactoryBean validator) {
            this.validator = validator;
        }

        @Override
        public void initBinder(WebDataBinder binder, WebRequest request) {
            binder.setValidator(validator);
    }
}

要创建LocalValidatorFactoryBean的实例,请使用mvc:annotation-drivenset up LocalValidatorFactoryBean as bean