开发者

Spring MVC validation in MultiActionController

I have been using Spring MVC for 4 months, but I still cannot validate my forms. This is what I am trying to do:

in my 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!" />

In my Controller (which extend 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");
    }

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

Of course my "myOrderTypeValidator" is:

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

And my Validator class (so far it checks only "title" description) :

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");
    }

}

Compiles ok and everything works,e xcept when I fill form I got this:

org.springframework.web.bind.ServletRequestBindingException: Errors binding onto object 'command'; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'command' on field 'title': rejected value []; codes [title.command.title,title.title,title.java.lang.String,title]; arguments []; default message [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)

root cause

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'command' on field 'title': rejected value []; codes [title.command.title,title.title,title.java.lang.String,title]; arguments []; default message [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(MultiActi开发者_StackOverflowonController.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)


may be u already fix your error but in case anyone else have same error u can try add a constructor in Model Class..

example :

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

i'm not sure if it will work to yours but it works in my project.

Thanx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜