Announcement Announcement Module
Collapse
No announcement yet.
spring-mvc validator not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring-mvc validator not working

    So, I'm trying to use JSR303 validator with HibernateValidator implementation in one of my Controllers but BindingResults always came "noErrors" even though it should complain about it.

    This is what I've got

    I added to my app-config.xml
    Code:
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
    I can see it poping out on the logs like
    Code:
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@8d6a3d1: defining beans.... org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0
    
    ...
    Nov 2, 2011 3:09:02 PM org.hibernate.validator.util.Version <clinit>
    INFO: Hibernate Validator 4.1.0.Final
    Nov 2, 2011 3:09:02 PM org.hibernate.validator.engine.resolver.DefaultTraversableResolver detectJPA
    INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.
    
    ....
    INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.
    Nov 2, 2011 3:09:50 PM org.springframework.web.context.ContextLoader initWebApplicationContext
    INFO: Root WebApplicationContext: initialization completed in 61784 ms
    The controller is as simple as
    Code:
    @RequestMapping(value="/user/signup", method=RequestMethod.POST)
    	public String processSignup(@Valid UserCommand command, BindingResult result) {
    		if (result.hasErrors())
    			return "/user/signup";
    		
    		return "redirect:/user/success";
    	}
    The UserCommand looks like this
    Code:
    public class UserCommand {
    	@NotNull
    	private String name;
    	
    	@NotNull
    	private String lastName;
    	
    	@Past
    	private Date dob;
    }
    But binding results is always zero errors.

    What is that piece that I'm missing? (I'm using spring 3.0.5.RELEASE)

    cheers

  • #2
    Have you included this in your xml configuration:

    <mvc:annotation-driven/>

    Comment


    • #3
      Thanks jtsnr,

      I've just found out that since I've got a custom "AnnotationMethodHandlerAdapter" and not using the <mvc:annotation-driven/> somehow the automagically bind of the validator does not work.

      Does any body know if I can pass my validator instance to the "AnnotationMethodHandlerAdapter" ?

      Comment


      • #4
        I had the same problem. I got rid of it by removing an explicit dependency to javax.validation which was in my pom.xml for some reason.

        Comment


        • #5
          I have used the annotated validators in my own project and did not have to define any validator beans that you have

          <bean id="validator" class="org.springframework.validation.beanvalidati on.LocalValidatorFactoryBean"/>
          I just put the following in my servletname-servlet.xml config file.

          Code:
              <context:component-scan  base-package="com.telecoms"/> 
              <context:annotation-config/>
              <mvc:annotation-driven/>

          Comment


          • #6
            Hi,

            you can found an todos application server sample implementing data validation on RESTful service @github

            Best regards
            Sorriso

            Comment

            Working...
            X