2

我有使用注释验证在春季3 mvc的问题。我不确定这些问题是否出现在验证中,或Spring是否有能力将验证错误绑定到BindingResult对象。Spring 3 MVC JSR-303验证没有显示错误

这里是我的控制器的方法

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

而且here's我的表单对象

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中完成的方式不同,有人可以解释它吗?以下是我的Junit测试。

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

回答

0

对于这项工作的JSR 303提供商(冬眠如验证)的实现必须存在于你的classpath。如果存在的话,它会在春季自动收集。

+0

但不是正是我所拥有的,当我在我的POM.xml中声明hibernate 4.0.2.GA验证程序 – 2011-03-25 07:00:30

+0

其他人对此问题有任何想法吗? – 2011-03-28 11:50:23