开发者

Spring 3 MVC JSR-303 validation doesn´t show errors

I have problem using annotated validation in spring 3 mvc. I´m not sure if the problems is in validation or Spring´s capability to bind validation errors to BindingResult object.

Here is my Controller's method

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

And here´s my form object

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

Here´s a part of my appContext (and yes.. Everything else works fine)

   <?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/>

I have Hibernate Validator and Validation API in my classpath

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

Problem is that i can call this test-method with valid url ("/test?test=thisIsValidInput") and with invalid url ("/test?test=a"). Both of the times BindingResult object has no errors. EDITED: I think i have the JSR-303 Validator provider in my classpath. I also have been able to run Junit test to my validator constraints. If the Validator loading is done differently in Spring, can someone explain it? Below is my Junit test.

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);
    A开发者_StackOverflowssert.assertEquals(1, errors.size());
    logger.debug(errors.toString());
}

In console it says:

         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


For this to work an implementation of the jsr 303 provider (hibernate validator for eg) needs to be present on your classpath. It is automatically picked up by spring if one exists.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜