我在有關日期驗證的麻煩在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";
}
消息Spring MVC的日期格式無效的錯誤消息。屬性
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:codes [clientForm.bday,bday];參數[];默認信息[bday]];默認消息[無法將屬性'java.lang.String'的屬性值轉換爲屬性'bday'所需的類型'java.util.Date';嵌套的異常是org.springframework.core.convert.ConversionFailedException:無法將類型爲'java.lang.String'的值「21/05e/2011」轉換爲類型'java.util.Date';嵌套異常是java.lang.IllegalArgumentException:無效格式:「2011/05/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) 有機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:codes [clientForm.bday,bday];參數[];默認信息[bday]];默認消息[無法轉換'java.lang'類型的屬性值。String'爲屬性'bday'所需的類型'java.util.Date';嵌套的異常是org.springframework.core.convert.ConversionFailedException:無法將類型爲'java.lang.String'的值「21/05e/2011」轉換爲類型'java.util.Date';嵌套異常是java.lang.IllegalArgumentException:無效格式:「2011/05/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) 組織。 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(DispatcherSer vlet.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) 有機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.SubjectCalla ble.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)
我故意在日期中放入'e',我試圖讓春天在日期驗證中顯示我的錯誤消息 – Kevin