Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring portlet mvc: @Valid does not seem to work

I created a bean class and use it in my controller but it does not seem to work. Namely even though I enter an invalid age, result.hasErrors is still false.

Bean class:

public class User{
    @Min(13)
    private int age;
    private String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName(){
            return name;
    }

    public void setName(String name){
            this.name = name;
    }   
}

Controller snippet:

@ActionMapping(params = "myAction=validateUser")
    public void validateUser(ActionRequest request, ActionResponse response, ModelMap model, @ModelAttribute("user") @Valid User user, BindingResult result ){      

        if(result.hasErrors()){
            for(ObjectError oe : result.getAllErrors()){
                System.out.println(oe.getDefaultMessage());
            }
        } else{
            //code
        }
    }

JSP:

<form:form action="${registerUser}" method="post" commandName="user">
    <b>User</b> 
    <form:input path="age"/>
    <form:input path="name"/>
    <input type="submit" value="register"/>
</form:form>

edit: My userRegistration-portlet.xml:

<?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:util="http://www.springframework.org/schema/util"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/mvc
        ">

    <mvc:annotation-driven /> 

    <import resource="spring-hibernate.xml"/>

    <context:component-scan base-package="comjohndoe.dao" />
    <context:component-scan base-package="comjohndoe.model" />
    <context:component-scan base-package="comjohndoe.service" />
    <context:component-scan base-package="comjohndoe.util" />
    <context:component-scan base-package="comjohndoecontroller" />

    <bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

It's the mvc:spring-validation line that is giving me the: cvc-complex-type.2.4.c the matching wildcard is strict, but no declaration can be found for the element mvc:annotation-driven. error.

like image 634
jack Avatar asked Nov 26 '10 08:11

jack


3 Answers

I had exactly the same problem. I read https://jira.springframework.org/browse/SPR-6817 and worked the problem around by adding to my config:

<mvc:annotation-driven validator="validator" />

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

<bean id="annotationMethodHandlerAdapter" class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="webBindingInitializer">
        <bean id="configurableWebBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <property name="validator">
                <ref bean="validator"/>
            </property>
        </bean>
    </property>
</bean>

You also need to have a JSR-303 validator in your project in order for it to be available For this I used Hibernate Validator: http://www.hibernate.org/subprojects/validator.html

like image 128
heikkim Avatar answered Nov 15 '22 12:11

heikkim


You need <mvc:annotation-driven /> to enable jsr-303 validation. If you don't want to use it for some reason, or if at some point it starts creating problems (like it did for me), take a look at this question

Update: in schemaLocation the mvc entry should contain these two:

http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
like image 26
Bozho Avatar answered Nov 15 '22 13:11

Bozho


You need to do the following, that is how I solved in the spring portlet mvc project.

  1. Add mvc:annotation-driven to your applicatonContext.xml

  2. Add the following code in your controller:

    @Autowired private LocalValidatorFactoryBean validator;

    @InitBinder public void initBinder(WebDataBinder binder) { binder.setValidator(validator); }

like image 29
Andy Avatar answered Nov 15 '22 12:11

Andy