English 中文(简体)
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...

最佳回答

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";
问题回答

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





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签