我有使用註釋驗證在春季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
但不是正是我所擁有的,當我在我的POM.xml中聲明hibernate 4.0.2.GA驗證程序 – 2011-03-25 07:00:30
其他人對此問題有任何想法嗎? – 2011-03-28 11:50:23