mgrstnr
mgrstnr

Reputation: 520

Spring Validator does not support command class

I think the issue is pretty common, but for some reason I cannot manage to fix this.

This is the error I am getting:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reverseController' defined in ServletContext resource [/WEB-INF/app-servlet.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Validator [FormValidator@6b3ded0d] does not support command class [ReverseString]

This is the supports method in my FormValidator class:

public boolean supports(Class clazz) {
    return ReverseController.class.isAssignableFrom(clazz);
}

This is the bean definition on my app-servlet.xml:

<bean id="reverseController" class="ReverseController">     
   <property name="commandName"><value>reverseString</value></property>
   <property name="commandClass"><value>ReverseString</value></property>        
   <property name="formView"><value>reverse</value></property>
   <property name="successView"><value>reverseResult</value></property>
   <property name="validator"><bean class="FormValidator" /></property>

And finally, this is the main part of my ReverseController:

 @Service
public class ReverseController extends SimpleFormController {

    public ReverseController() {
        //setCommandClass(ReverseString.class);
        //setCommandName("reverseString");
    }

    private ReverseString reverseStringMaster;

    @Autowired
    public void setWriter(ReverseString reverseStringMaster) {
        this.reverseStringMaster = reverseStringMaster;
    }

    protected ModelAndView onSubmit(HttpServletRequest request,
        HttpServletResponse response, Object command, BindException errors) {

        ReverseString revString = (ReverseString) command;

        return new ModelAndView(getSuccessView(),"reversedString",  revString);
    }

    public void init() {
        System.out.println("Done");
    }


}

Any idea what might be causing that issue?

Upvotes: 0

Views: 2016

Answers (3)

Will Keeling
Will Keeling

Reputation: 23004

It's an instance of the command class that gets validated on each request - as this gets populated with form data. The controller itself is not validated.

So the supports() method in your FormValidator should actually read:

public boolean supports(Class clazz) {
    return ReverseString.class.isAssignableFrom(clazz);
}

Upvotes: 3

vijayk
vijayk

Reputation: 2753

Try this code for your app-servlet.xml
1.Define your controller like this.
2.then use reverseController to call ReverseController class method.

<managed-bean>
        <managed-bean-name>reverseController</managed-bean-name>
        <managed-bean-class>com.action.ReverseController</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>

Upvotes: 0

Shreyos Adikari
Shreyos Adikari

Reputation: 12744

Did you add getters and setters for this commandClass variable as well as well?
If so, you probably need to remove the @spring.validator type="required" from the setter method method.

Upvotes: 0

Related Questions