Issue
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...
Solution
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";
Answered By - axtavt