以下是input
元素上id为endDate
$('#endDate').datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'mm/yy',
});
删除日期部分,只有月份和年份以' MM / YYYY'的格式显示。
我已将此元素绑定到beans属性,如下所示。
<form:input id="endDate" onkeydown="return false;" readonly="readonly" path = "endDate" />
在beans属性中,我使用了org.springframework.format.annotation.DateTimeFormat
,如下所示
@DateTimeFormat(pattern = "MM/yyyy")
private Date endDate;
datepicker正常工作,但是当表单提交给控制器时,我得到以下异常。
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'SpringWeb' on field 'endDate': rejected value [12/2014]; codes [typeMismatch.SpringWeb.endDate,typeMismatch.endDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [SpringWeb.endDate,endDate]; arguments []; default message [endDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "12/2014" from type 'java.lang.String' to type 'java.util.Date'; nested exception is java.lang.IllegalArgumentException: Invalid format: "12/2014" is malformed at "14"]
Field error in object 'SpringWeb' on field 'startDate': rejected value [12/2014]; codes [typeMismatch.SpringWeb.startDate,typeMismatch.startDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [SpringWeb.startDate,startDate]; arguments []; default message [startDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'startDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "12/2014" from type 'java.lang.String' to type 'java.util.Date'; nested exception is java.lang.IllegalArgumentException: Invalid format: "12/2014" is malformed at "14"]
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doBind(HandlerMethodInvoker.java:810)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:359)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)
是什么原因?什么可以解决?
答案 0 :(得分:1)
从您的堆栈跟踪中,您可以看到消息Invalid format: "12/2014" is malformed at "14"
您需要将private Date endDate
的模式从"MM/yyyy"
更改为"MM/yy"
,因为yyyy
表示以4表示的年份像2014
这样的数字,但是你在2位数14
答案 1 :(得分:0)
为了解决这个问题,我在控制器中实现了一个InitBinder方法,为特定字段注册了一个自定义编辑器:
@InitBinder
public void binder(WebDataBinder binder) {
DateFormat dateOnlyFormat = new SimpleDateFormat("dd/MM/yyyy");
dateOnlyFormat.setLenient(true);
binder.registerCustomEditor(Date.class, "dataInicio", new CustomDateEditor(dateOnlyFormat, true));
}