MultiActionController中的Spring MVC验证

时间:2011-08-30 11:50:50

标签: validation spring-mvc

我已经使用Spring MVC 4个月了,但我还是无法验证我的表单。这就是我想要做的事情:

在我的jsp中:

<form:form method="POST" action="ordertypeadd.htm" commandName="orderType">
ID Parent: <form:input path="id_parent"/><form:errors path="id_parent" /><br/>
Name: <form:input path="title"/> <form:errors path="title" /> <br/>
Description: <form:input path="description"/> <form:errors path="description" /><br/>
<input type="submit" value="Submit!" />

在我的Controller(扩展MultiActionController)中

@RequestMapping(value="/ordertypeadd.htm", method=RequestMethod.POST)
    public ModelAndView ordertypeadd(HttpServletRequest request,
            HttpServletResponse response, OrderType orderType)throws Exception{
            orderTypeDAO.save(orderType);
            return new ModelAndView("redirect:ordertypelist.htm");
    }

在我的spring-servlet.xml中:

<bean name="/ordertypeadd.htm" class="pl.edm.orders.spring.OrderTypeController">
        <property name="orderTypeDAO" ref="myOrderTypeDAO" />
        <property name="validators">
        <list>
        <ref bean="myOrderTypeValidator"/>
        </list>
        </property>
    </bean>

当然我的“myOrderTypeValidator”是:

<bean id="myOrderTypeValidator" class="pl.edm.orders.spring.OrderTypeValidator">

我的Validator类(到目前为止只检查“标题”描述):

public class OrderTypeValidator implements Validator{

    public boolean supports(Class arg0) {
        return OrderType.class.equals(arg0);
    }

    public void validate(Object arg0, Errors arg1) {
        ValidationUtils.rejectIfEmpty(arg1, "title", "title");
    }

}

编译确定,一切正常,除非我填写表格,我得到了这个:

  

org.springframework.web.bind.ServletRequestBindingException:错误   绑定到对象'命令';嵌套异常是   org.springframework.validation.BindException:   org.springframework.validation.BeanPropertyBindingResult:1个错误   字段'title'上对象'command'中的字段错误:被拒绝的值[];   代码[title.command.title,title.title,title.java.lang.String,title];   参数[];默认消息[null]     org.springframework.web.bind.ServletRequestDataBinder.closeNoCatch(ServletRequestDataBinder.java:122)     org.springframework.web.servlet.mvc.multiaction.MultiActionController.bind(MultiActionController.java:540)     org.springframework.web.servlet.mvc.multiaction.MultiActionController.invokeNamedMethod(MultiActionController.java:468)     org.springframework.web.servlet.mvc.multiaction.MultiActionController.handleRequestInternal(MultiActionController.java:408)     org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)     org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)     org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)     org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)     org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:585)     javax.servlet.http.HttpServlet.service(HttpServlet.java:641)     javax.servlet.http.HttpServlet.service(HttpServlet.java:722)     org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)     org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

     

根本原因

     

org.springframework.validation.BindException:   org.springframework.validation.BeanPropertyBindingResult:1个错误   字段'title'上对象'command'中的字段错误:被拒绝的值[];   代码[title.command.title,title.title,title.java.lang.String,title];   参数[];默认消息[null]     org.springframework.web.bind.ServletRequestDataBinder.closeNoCatch(ServletRequestDataBinder.java:124)     org.springframework.web.servlet.mvc.multiaction.MultiActionController.bind(MultiActionController.java:540)     org.springframework.web.servlet.mvc.multiaction.MultiActionController.invokeNamedMethod(MultiActionController.java:468)     org.springframework.web.servlet.mvc.multiaction.MultiActionController.handleRequestInternal(MultiActionController.java:408)     org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)     org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)     org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)     org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)     org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:585)     javax.servlet.http.HttpServlet.service(HttpServlet.java:641)     javax.servlet.http.HttpServlet.service(HttpServlet.java:722)     org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)     org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

1 个答案:

答案 0 :(得分:0)

可能已经修复了你的错误但是如果其他人有同样的错误,你可以尝试在Model Class中添加一个构造函数。

示例:

public Class Person{
  private String id;
  private String name;
  public Person(String id){
     this.id=id;
  }
  //setter getter and default constructor here
}

我不确定它是否适用于你的,但它适用于我的项目。

感谢名单