Spring 3 MVC JSR-303验证没有显示错误

时间:2011-03-24 09:02:52

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

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

这是我的Controller的方法

@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对象都没有错误。 编辑: 我想我的classpath中有JSR-303 Validator提供程序。我也能够运行Junit测试到我的验证器约束。如果在Spring中以不同方式完成Validator加载,有人可以解释一下吗?以下是我的Junit测试。

private static 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

1 个答案:

答案 0 :(得分:0)

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

相关问题