Spring MVC 无效日期格式错误消息未显示
我在 Spring MVC 3
ClientForm.java
public class ClientForm
{
private Date bday = new Date();
//Getters and setters
}
中的日期验证方面遇到问题在我的控制器
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@Valid ClientForm form, BindingResult result)
{
if(result.hasErrors())
{
return "client.form";
}
return "redirect:search";
}
messages.properties
client.search.notnumber=Search value must be a number
typeMismatch.java.util.Date = Invalid date
jsp
<form:form action="save.html" method="post" commandName="clientForm">
<form:input path="bday" cssClass="date-pick dp-applied" />
<form:errors path="bday" element="label" cssClass="error"/>
</form:form>
“messages.properties”中的其他验证消息工作正常 但是当我故意输入无效日期(即 111/12/2011) 时,
我收到此错误消息
org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 错误 字段“bday”上对象“clientForm”中的字段错误:拒绝值 [21/05e/2011];代码 [typeMismatch.clientForm.bday,typeMismatch.bday,typeMismatch.java.util.Date,typeMismatch];参数 [org.springframework.context.support.DefaultMessageSourceResolvable: 代码 [clientForm.bday,bday];参数[];默认消息[bday]];默认消息[无法将类型“java.lang.String”的属性值转换为属性“bday”所需的类型“java.util.Date”;嵌套异常是 org.springframework.core.convert.ConversionFailedException:无法将值“21/05e/2011”从类型“java.lang.String”转换为类型“java.util.Date”;嵌套异常是 java.lang.IllegalArgumentException:格式无效:“21/05e/2011”在“e/2011”处格式错误] org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560) javax.servlet.http.HttpServlet.service(HttpServlet.java:637) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:359) org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:275) org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:344) org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:272) org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:81) org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237) org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167) 根本原因 org.springframework.validation.BindException:org.springframework.validation.BeanPropertyBindingResult:1个错误 字段“bday”上对象“clientForm”中的字段错误:拒绝值 [21/05e/2011];代码 [typeMismatch.clientForm.bday,typeMismatch.bday,typeMismatch.java.util.Date,typeMismatch];参数 [org.springframework.context.support.DefaultMessageSourceResolvable: 代码 [clientForm.bday,bday];参数[];默认消息[bday]];默认消息[无法将类型“java.lang.String”的属性值转换为属性“bday”所需的类型“java.util.Date”;嵌套异常是 org.springframework.core.convert.ConversionFailedException:无法将值“21/05e/2011”从类型“java.lang.String”转换为类型“java.util.Date”;嵌套异常是 java.lang.IllegalArgumentException:格式无效:“21/05e/2011”在“e/2011”处格式错误] org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doBind(HandlerMethodInvoker.java:810) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:359) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:153) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560) javax.servlet.http.HttpServlet.service(HttpServlet.java:637) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:359) org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:275) org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:344) org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:272) org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:81) org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237) org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
I'm having trouble regarding Date validation in Spring MVC 3
ClientForm.java
public class ClientForm
{
private Date bday = new Date();
//Getters and setters
}
In my Controller
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@Valid ClientForm form, BindingResult result)
{
if(result.hasErrors())
{
return "client.form";
}
return "redirect:search";
}
messages.properties
client.search.notnumber=Search value must be a number
typeMismatch.java.util.Date = Invalid date
jsp
<form:form action="save.html" method="post" commandName="clientForm">
<form:input path="bday" cssClass="date-pick dp-applied" />
<form:errors path="bday" element="label" cssClass="error"/>
</form:form>
Other validation messages in 'messages.properties' works fine
but when i intentionally typed an invalid date (i.e. 111/12/2011)
i got this error message
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'clientForm' on field 'bday': rejected value [21/05e/2011]; codes [typeMismatch.clientForm.bday,typeMismatch.bday,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [clientForm.bday,bday]; arguments []; default message [bday]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'bday'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "21/05e/2011" from type 'java.lang.String' to type 'java.util.Date'; nested exception is java.lang.IllegalArgumentException: Invalid format: "21/05e/2011" is malformed at "e/2011"]
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:359)
org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:275)
org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:344)
org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:272)
org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:81)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
root cause
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'clientForm' on field 'bday': rejected value [21/05e/2011]; codes [typeMismatch.clientForm.bday,typeMismatch.bday,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [clientForm.bday,bday]; arguments []; default message [bday]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'bday'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "21/05e/2011" from type 'java.lang.String' to type 'java.util.Date'; nested exception is java.lang.IllegalArgumentException: Invalid format: "21/05e/2011" is malformed at "e/2011"]
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doBind(HandlerMethodInvoker.java:810)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:359)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:153)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:359)
org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:275)
org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:344)
org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:272)
org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:81)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个具有约束力的例外。你定义了活页夹吗?如果是,您是否已注册日期编辑?也许你应该检查那个编辑器,你可能有一个拼写错误......日期中的“e”来自哪里?对我来说这似乎是一个转换错误。
This is a binding exception. Have you defined a binder? If yes, have you registered an editor for dates? Perhaps you should check that editor, you may have a typo there... where is that 'e' in the date coming from? It seems like a conversion error to me.