14

I have specified <mvc:annotation-driven /> in dispatcher-servlet. I am not using @InitBinder.
And I am using @valid annotation for validation in controller's method like

@RequestMapping(method = RequestMethod.POST, value = "new")
    public String save(@Valid Article article,ModelMap model) {
//code here
}

And validation works fine, but instead of showing error in .. sample shown in html code

<tr>
         <td>Title</td>
         <td><form:input path="title"/></td>
         <td><form:errors path="title"/></td>
</tr>

It throws exception like..

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors Field error in object 'article' on field 'urlInfo.url': rejected value []; codes [typeMismatch.article.urlInfo.url,typeMismatch.urlInfo.url,typeMismatch.url,typeMismatch.java.net.URL,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [article.urlInfo.url,urlInfo.url]; arguments []; default message [urlInfo.url]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.net.URL' for property 'urlInfo.url'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value from type 'java.lang.String' to type 'java.net.URL'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value from type 'java.lang.String' to type 'java.net.URL'; nested exception is java.lang.reflect.InvocationTargetException] Field error in object 'article' on field 'title': rejected value []; codes [Size.article.title,Size.title,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [article.title,title]; arguments []; default message [title],{javax.validation.constraints.Size.message},6,[Ljava.lang.Class;@1db3aac,2147483647,[Ljava.lang.Class;@1e90abf]; default message [size must be between 6 and 2147483647]

 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:659)
 org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
 org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:213)
 org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:171)
 org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
 org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
 org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)
 org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

How to configure it, to not throw an exception and instead return to page and show error messages...

1
  • How have you enabled the validation? Using @InitBinder on your controller, or <mvc:annotation-driven/> in your context? Whichever one you're using, please add those bits to your question. Commented Jan 16, 2010 at 11:51

2 Answers 2

18

In your controller handler method, make sure that the BindingResult argument is immediately after the command argument.

Sign up to request clarification or add additional context in comments.

2 Comments

Very strange but this worked for me as well. I had always been told that Spring can figure out controller method parameters on its own and I would never need to worry about them! But this is at the least one instance where the order of the parameters impacts the execution!
Yes, the BindingResult has to be placed exactly after the input(request) attribute. Worked for me as well.
16

You should explicitly decide what to do with validation errors:

@RequestMapping(method = RequestMethod.POST, value = "new") 
public String save(@Valid Article article, BindingResult result, ModelMap model) { 
    if (result.hasErrors())
        return "formView";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.