0

我正在使用 jquery 日期选择器在 UI 中提供日期。并提到 dateFormat 为

$( "#validFrmDt" ).datepicker({ dateFormat: "dd-mm-yy" });

对于以下要求的格式:例如:30-09-2013

在我的 spring mvc 模型类中,我希望这种格式与我的 java.util.Date 属性绑定,为此我配置如下:

@DateTimeFormat(pattern = "dd-MM-yyyy")
private Date validFromDate;

但不幸的是,我得到了以下异常:

SEVERE: Servlet.service() for servlet mvc-dispatcher threw exception
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'submitXXXForm' on field 'validFromDate': rejected value [30-09-2013]; codes [typeMismatch.submitXXXForm.validFromDate,typeMismatch.validFromDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [submitXXXForm.validFromDate,validFromDate]; arguments []; default message [validFromDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'validFromDate'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'validFromDate': no matching editors or conversion strategy found]
Field error in object 'submitXXXForm' on field 'validToDate': rejected value [30-09-2013]; codes [typeMismatch.submitXXXForm.validToDate,typeMismatch.validToDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [submitXXXForm.validToDate,validToDate]; arguments []; default message [validToDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'validToDate'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'validToDate': no matching editors or conversion strategy found]
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doBind(HandlerMethodInvoker.java:818)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:367)
    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:923)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    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:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    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:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)

是我的日期格式配置不对。请帮忙。

4

1 回答 1

0

您需要有一个自定义转换器才能从 String 转换为 java.util.Date 。在您的应用程序上下文中,您可能有这样的事情:

<bean id="customEditorsRegistrar" class="org.springframework.batch.support.DefaultPropertyEditorRegistrar">
    <property name="customEditors" ref="customEditors" />
</bean>

如果这样做,则需要创建自定义编辑器并将其添加到 HashMap 以将转换器从 String 转换为 java.util.Date :

<bean id="customEditors" class="java.util.HashMap">
    <constructor-arg>
        <map>
            <entry key="java.util.Date">
                <bean class="org.springframework.beans.propertyeditors.CustomDateEditor">
                    <constructor-arg>
                        <bean class="pt.xx.xxxx.xxxx.core.common.support.file.converter.UtilDateFormat" />
                    </constructor-arg>
                    <constructor-arg value="true" />
                </bean>
            </entry>
        </map>
    </constructor-arg>
</bean>
于 2013-12-05T20:06:51.800 回答