Announcement Announcement Module
No announcement yet.
Problem integrating HibernateValidator in a Spring Portlet context Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem integrating HibernateValidator in a Spring Portlet context

    I'd like to use the Hibernate Validator tool (based on JSR-303) in my Spring Portlet application by @Valid annotation, as is shown in this example:

    Later of a deep search about this topic I've found out that there's a recognised issue about it:

    Juergen Hoeller added a comment - 09/Feb/10 1:15 AM

    I'm afraid that this is a fundamental limitation in Spring 3.0: The MVC namespace works in a Servlet MVC environment only, i.e. within a DispatcherServlet. It has no effect in a Portlet environment. I've added a corresponding warning to the namespace docs.

    We'll revisit this for Spring 3.1. For the time being, in Spring 3.0, you'll have to configure an AnnotationMethodHandlerAdapter bean, passing a ConfigurableWebBindingInitializer into its "webBindingInitializer" property. That nested ConfigurableWebBindingInitializer bean in turn features "conversionService" and "validator" properties.
    So that's what I've done. I coded a CommonWebBindingInitializer class which recives a LocalValidatorFactoryBean and a ConversionService (both provided by the Spring framework) and sets them to the WebDataBinder. Thus, when I invoke a validation with the @Valid tag, the JSR-303 validation takes place.
    The validation recognise javax.validation and returns the Errors list but when I replaced them with org.hibernate.validator annotations, the Errors list is empty. What am I doing wrong?

    I have included the hibernate-validator-4.2.0.Final.jar in the classpath. It seems I don't understanding how the LocalValidatorFactorybean works. Please, Some help?
    Last edited by paul_phoenix; Jan 24th, 2012, 01:28 AM.

  • #2
    I've added the following validation.xml configuration file (extracted from the Hibernate Validator reference guide):

    <validation-config xmlns=""
    The node default-provider allows to choose the Bean Validation provider. This is useful if there is more than one provider in the classpath

    Then, I get a Validator instance in my Controller by doing:
    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    BeanDescriptor descriptor = validator.getConstraintsForClass(ConsultaPedidoEmpForm.class);
    I've checked the type of the validator which is "org.hibernate.validator.engine.ValidatorImpl" and when I invoke the validation, it is supposed I should obtain a Set of ConstraintViolation but I only get the Violations for the fields annotated with javax.validation.constraints kind constraints. What about the org.hibernate.validator constraints?? Why the latests aren't recognised for the validator??
    Set<ConstraintViolation<ConsultaPedidoEmpForm>> errors = validator.validate(empForm);
    Please, someone who could give me a little clue... I almost have It!