Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not showing error messages when validated using @valid(JSR-303) in Spring MVC

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

like image 870
hemal Avatar asked Jan 16 '10 11:01

hemal


People also ask

How do I customize default error message from Spring @valid validation?

You can perform validation with Errors/BindingResult object. Add Errors argument to your controller method and customize the error message when errors found. Below is the sample example, errors. hasErrors() returns true when validation is failed.

What is the use of @valid Annotation in Spring MVC?

The @Valid annotation ensures the validation of the whole object. Importantly, it performs the validation of the whole object graph. However, this creates issues for scenarios needing only partial validation. On the other hand, we can use @Validated for group validation, including the above partial validation.

What is JSR 303 Bean Validation?

Bean Validation. JSR-303 bean validation is an specification whose objective is to standardize the validation of Java beans through annotations. The objective of the JSR-303 standard is to use annotations directly in a Java bean class.


2 Answers

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

like image 76
Prateek Avatar answered Oct 02 '22 11:10

Prateek


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";
like image 39
axtavt Avatar answered Oct 02 '22 12:10

axtavt