gpt4 book ai didi

java - Spring DateTimeFormat 不适用于 jQuery UI Datepicker

转载 作者:行者123 更新时间:2023-12-01 12:05:54 24 4
gpt4 key购买 nike

下面是 id 为 endDate 的 input 元素上日期选择器的代码

 $('#endDate').datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'mm/yy',
});

它删除了日期部分,仅以“MM/YYYY”的格式显示月份和年份。

我已将此元素绑定(bind)到 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;

日期选择器按预期工作正常,但是当表单提交到 Controller 时,我收到以下异常。

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)

原因是什么?有什么解决办法吗?

最佳答案

从堆栈跟踪中,您可以看到消息格式无效:“12/2014”在“14”处格式错误您需要将私有(private)日期 endDate 的模式从 "MM/yyyy""MM/yy" 因为 yyyy 表示以 4 位数字表示的年份,例如 2014,但是您以 2 位数表示年份 14

关于java - Spring DateTimeFormat 不适用于 jQuery UI Datepicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27603625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com