【问题标题】:Bean Validation does't workBean 验证不起作用
【发布时间】:2015-04-20 21:13:53
【问题描述】:

我目前正在学习 spring,但我坚持使用不适用于我的 bean 的验证注释。我真的不明白缺少什么,我需要一个手 :)

我有一个控制器:

@Controller
public class CirclesController {

    @RequestMapping(value = "/createCircle", method = RequestMethod.POST)
    public ModelAndView createCircle(@Valid Circle circle, BindingResult res) {

        if (res.hasErrors()) {
            System.out.println("Can't validate this form..");
        else
            System.out.println("Created new circle : " + circle);
    }
}

还有一颗豆子:

public class Circle {

    @Size(min = 5)                 // This is what I try to validate
    private String      name;

public String getName() {
        return name;
    }

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

我配置了 web.xml

<listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:conf/dao-context.xml
        classpath:conf/services-context.xml
        classpath:conf/beans-context.xml
    </param-value>
</context-param>

我的项目看起来像这样:

*-context.xml 有 component-scan 和 annotation-config 标签:

<context:component-scan base-package="com.test.app.[package-name]">
</context:component-scan>
<context:annotation-config></context:annotation-config>
<tx:annotation-driven></tx:annotation-driven>

我拥有所有外部库(hibernate、hibernate-api、javax.validation)并且运行时没有错误... 但是当我用少于 5 个字符填写“名称”字段时,我总是得到“创建新圈子:圈子 {name=txt}”而不是“无法验证此表单..”。

编辑:

这是我的类路径:

和 servlet-context.xml :

<context:component-scan base-package="com.test.app.controllers"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>

<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsps/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

【问题讨论】:

  • Circle{name=NomDuCercle} 这个名字是从哪里来的。超过 5 个字符!
  • 愚蠢的我,我写了一个糟糕的例子!我更新了这个问题^^

标签: java spring hibernate spring-mvc bean-validation


【解决方案1】:

提供您的依赖项列表和 circles-servlet.xml 将为您的问题提供完整的上下文。

尽管如此,据我所知,可能只有两件事丢失了。首先确保您的类路径中有验证提供程序,例如 hibernate-validator,其次确保您有

 <mvc:annotation-driven />
circles-servlet.xml 中的

元素,它支持启用对带有 @Valid

注释的控制器的参数对象进行验证

评论后更新

bean 验证具有更新的规范,因此您应该按照以下方式调整依赖项

hibernate-validator-5.x.x 
validation-api-1.1.x

这将实现 JSR-349

hibernate-validator-4.x.x
validation-api-1.0.x.

它实现了 JSR-303

评论中的问题意味着您很可能混合了依赖项,因此将 hibernate-validator-5.x.x 与 validation-api-1.0.x 一起使用,或者以相反的方式错过它

【讨论】:

  • 感谢您的建议,我使用 maven 添加了此依赖项,然后在 WEB-INF/lib 中添加了此错误:严重:向 org.springframework 类的侦听器实例发送上下文初始化事件的异常。 web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean#0”的bean时出错:调用init方法失败;嵌套异常是 java.lang.NoClassDefFoundError: javax/validation/ParameterNameProvider,知道吗?非常感谢!
  • 见这里:依赖不匹配:stackoverflow.com/questions/24652753/…
  • 正是我的观点,投赞成票(投赞成票,不要误会:))
  • 宾果游戏!有用 !我遵循的教程没有提到这个依赖,也感谢你@AlanHay :) 祝你有美好的一天!
【解决方案2】:

查看此页面的最底部:

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html

将以下内容添加到您的 spring 配置中:

<!-- JSR-303/JSR-349 support will be detected on classpath and enabled automatically -->
    <mvc:annotation-driven/>

【讨论】:

  • 谢谢你,但它仍然不起作用:S 我认为 工作相同..
  • 涉及事务,与此无关。正如其他答案中所建议的,请确保您在类路径上有一个验证提供程序。
  • 是的,我忘记了为什么我有这个 tx:annotation-driven ^^
【解决方案3】:

将@RequestBody 注释与@Valid 注释一起使用

public ModelAndView createCircle(@Valid @RequestBody Circle circle, BindingResult res) {

【讨论】:

  • 它和 spring 上下文文件中的 行一起工作正常。
猜你喜欢
  • 1970-01-01
  • 2023-03-04
  • 2014-05-29
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多