无法为@DateTimeFormat Spring 3正确配置

时间:2013-09-30 07:29:19

标签: spring spring-mvc spring-3 datetime-format

我正在使用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)

我的日期格式配置是否错误。请帮助。

1 个答案:

答案 0 :(得分:1)

您需要有一个自定义转换器才能从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>