我认为这个问题很普遍,但是由于某些原因,我无法解决此问题。

这是我得到的错误:

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]


这是我的FormValidator类中的supports方法:

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


这是我的app-servlet.xml中的bean定义:

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




最后,这是我的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");
    }


}


任何想法可能导致该问题吗?

最佳答案

它是命令类的一个实例,该类在每次请求时均得到验证-因为该类中填充了表单数据。控制器本身未经验证。

因此,您的supports()中的FormValidator方法应实际显示为:

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

关于java - Spring Validator不支持命令类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16665988/

10-10 02:56