java 在 Spring MVC 中使用 @valid(JSR-303) 验证时不显示错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2077213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Not showing error messages when validated using @valid(JSR-303) in Spring MVC
提问by hemal
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
我已经<mvc:annotation-driven />在 dispatcher-servlet 中指定 了。我没有使用@InitBinder。
我在控制器的方法中使用@valid 批注进行验证,例如
@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
验证工作正常,但不是在 .. html 代码中显示的示例中显示错误
<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.util.NestedServletException:请求处理失败;嵌套异常是 org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 个错误字段 'urlInfo.url' 对象 'article' 中的字段错误:拒绝值 []; 代码 [typeMismatch.article.urlInfo.url,typeMismatch.urlInfo.url,typeMismatch.url,typeMismatch.java.net.URL,typeMismatch]; 参数 [org.springframework.context.support.DefaultMessageSourceResolvable: 代码 [article.urlInfo.url,urlInfo.url]; 参数 []; 默认消息 [urlInfo.url]]; 默认消息 [无法将“java.lang.String”类型的属性值转换为属性“urlInfo.url”所需的类型“java.net.URL”;嵌套异常是 org.springframework.core.convert。ConversionFailedException:无法将值从“java.lang.String”类型转换为“java.net.URL”类型;嵌套异常是 org.springframework.core.convert.ConversionFailedException: Unable to convert value from type 'java.lang.String' to type 'java.net.URL'; 嵌套异常是 java.lang.reflect.InvocationTargetException] 字段 'title' 上的对象 'article' 中的字段错误:拒绝值 []; 代码 [Size.article.title,Size.title,Size.java.lang.String,Size]; 参数 [org.springframework.context.support.DefaultMessageSourceResolvable: 代码 [article.title,title]; 参数 []; 默认消息 [title],{javax.validation.constraints.Size.message},6,[Ljava.lang.Class;@1db3aac,2147483647,[Ljava.lang.Class;@1e90abf];
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...
如何配置它,不抛出异常而是返回页面并显示错误消息...
回答by Prateek
In your controller handler method, make sure that the BindingResult argument is immediately after the command argument.
在您的控制器处理程序方法中,确保 BindingResult 参数紧跟在命令参数之后。
回答by axtavt
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";

