【问题标题】:Spring portlet mvc: @Valid does not seem to workSpring portlet mvc:@Valid 似乎不起作用
【发布时间】:2011-05-16 01:53:44
【问题描述】:

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

豆类:

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;
    }   
}

控制器sn-p:

@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. 错误。

【问题讨论】:

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


    【解决方案1】:

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

    更新: 在schemaLocation 中,mvc 条目应该包含这两个

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

    【讨论】:

    • 感谢您的提示,我尝试将其放在我的 userRegistration-portlet.xml 中,但我得到一个:mvc:annotation-driven 的前缀 mvc 未定义。但是当我谷歌 我没有看到任何差异?
    • 编辑:添加:xmlns:mvc="springframework.org/schema/mvc" 我还添加了:springframework.org/schema/mvc/spring-mvc-3.0.xsd 到我的架构位置。现在我得到错误:匹配的通配符是严格的,但是找不到元素 mvc:annotation-driven 的声明。抱歉发了两次帖子,但我的网速真的很慢。所以我也可能会错过一些回复。
    • 我想我可能缺少一个 Maven 依赖项,但我真的想不出一个。
    • @Jack - 发布你的 bean 定义 xml 文件。
    • @Bohzo,遗憾的是这并没有改变任何东西。
    【解决方案2】:

    我认为原因是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());
        }
    

    你也可以在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

    【讨论】:

    • 这比 mvc:annotation-drive 路由要冗长一些,但感谢您的意见。
    【解决方案3】:

    我遇到了完全相同的问题。我阅读了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 验证器:http://www.hibernate.org/subprojects/validator.html

    【讨论】:

    • 我希望早点找到这篇文章。 99% 的示例都缺少这一点关键信息。
    • 由于未知原因,该解决方案对我不起作用,但@Andy's 做到了!
    【解决方案4】:

    你需要做以下,这就是我在spring portlet mvc项目中解决的方法。

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

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

      @Autowired private LocalValidatorFactoryBean validator;

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-23
      • 2014-12-23
      • 1970-01-01
      相关资源
      最近更新 更多