【问题标题】:Spring 3 MVC JSR-303 validation doesn´t show errorsSpring 3 MVC JSR-303 验证不显示错误
【发布时间】:2011-07-21 23:18:02
【问题描述】:

我在 spring 3 mvc 中使用带注释的验证时遇到问题。我不确定问题出在验证中还是 Spring 将验证错误绑定到 BindingResult 对象的能力。

这是我的控制器的方法

@RequestMapping(value = "/test", method = RequestMethod.POST)
public String testingValidation(@Valid Test test,BindingResult result){

    if(result.hasErrors()){
        logger.debug("Error in validation "+result.toString());
        return "redirect:http://someWrongPlace.com";
    }

    logger.debug("No validation error "+result.toString());
    return "redirect:http://theRightPlace.com";
}

这是我的表单对象

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class Test {


   @NotNull
   @Size(min = 5)
   private String text;

   public String getText() {
    return text;
   }

   public void setText(String text) {
    this.text = text;
   }
}

这是我的 appContext 的一部分(是的。其他一切正常)

   <?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:amq="http://activemq.apache.org/schema/core"
    xmlns:jms="http://www.springframework.org/schema/jms"
    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/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://activemq.apache.org/schema/core 
    http://activemq.apache.org/schema/core/activemq-core.xsd
    http://www.springframework.org/schema/jms 
    http://www.springframework.org/schema/jms/spring-jms-3.0.xsd"
    default-autowire="byName">

    <mvc:annotation-driven/>

    <context:annotation-config/>

我的类路径中有 Hibernate Validator 和 Validation API

   <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>com.springsource.org.hibernate.validator</artifactId>
        <version>4.0.2.GA</version>
    </dependency>

问题是我可以用有效的 url ("/test?test=thisIsValidInput") 和无效的 url ("/test?test=a") 调用这个测试方法。两次 BindingResult 对象都没有错误。 编辑: 我想我的类路径中有 JSR-303 Validator 提供程序。我还能够对我的验证器约束运行 Junit 测试。如果验证器加载在 Spring 中以不同方式完成,有人可以解释一下吗?下面是我的 Junit 测试。

私有静态Validator验证器;

   Test test;

@Before
public void setUp(){

    ValidatorFactory factory=Validation.buildDefaultValidatorFactory();
    this.validator=factory.getValidator();
    test = new Test();
}


@Test
public void testTextIsNotNull(){

    test.setText("");

    Set<ConstraintViolation<Test>>errors = validator.validate(message);
    Assert.assertEquals(1, errors.size());
    logger.debug(errors.toString());
}

在控制台中显示:

         INFO [main] (Version.java:56) Hibernate Validator 4.0.2.GA 
         DEBUG [main](ResourceBundleMessageInterpolator.java:193)ValidationMessages
         not found. Delegating to org.hibernate.validator.ValidationMessages 
         DEBUG [main]defaultTraversableResolver.java:71)Cannot find 
         javax.persistence.PersistenceUtil on classpath. All properties will per 
         default be traversable. 
         DEBUG [main] (ValidationXmlParser.java:218) No META-INF/validation.xml found.
         Using annotation based configuration only

【问题讨论】:

    标签: hibernate model-view-controller spring validation annotations


    【解决方案1】:

    为此,您的类路径中需要存在 jsr 303 提供程序(例如,休眠验证器)的实现。如果存在,它会被 spring 自动拾取。

    【讨论】:

    • 但是当我在我的 POM.xml 中声明 hibernate 4.0.2.GA 验证器时,这不仅仅是我所拥有的
    • 还有其他人对这个问题有什么想法吗?
    猜你喜欢
    • 2014-09-28
    • 2011-05-10
    • 2012-02-08
    • 1970-01-01
    • 2018-08-15
    • 2011-04-30
    • 2013-11-23
    • 2013-12-27
    • 2013-01-31
    相关资源
    最近更新 更多