使用@DateTimeFormat注释进行Spring MVC JodaTime转换

时间:2015-03-17 14:15:20

标签: spring model-view-controller jodatime data-conversion

我有一个webflow项目,并且有一个带有DateTime字段的表单,当我提交表单和bindAndValidate时,我总是收到以下错误:

Field error in object 'formData' on field 'requestedDeadline': rejected value [2015-03-31]; codes [typeMismatch.formData.requestedDeadline,typeMismatch.requestedDeadline,typeMismatch.org.joda.time.DateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [formData.requestedDeadline,requestedDeadline]; arguments []; default message [requestedDeadline]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'org.joda.time.DateTime' for property 'requestedDeadline'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.joda.time.DateTime] for property 'requestedDeadline': no matching editors or conversion strategy found], 'messagesMemento' -> map[[null] -> list[[empty]]], 'org.springframework.validation.BindingResult.currentFormObject' -> org.springframework.validation.BeanPropertyBindingResult: 1 errors

这是我的servlet上下文:

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<context:component-scan base-package="com.acme" />

<!-- Resolves localized messages*.properties and application.properties 
    files in the application to allow for internationalization. The messages*.properties 
    files translate Roo generated messages which are part of the admin interface, 
    the application.properties resource bundle localizes all application specific 
    messages such as entity names and menu items. -->
<beans:bean
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
    id="messageSource" p:basenames="WEB-INF/i18n/messages,WEB-INF/i18n/application"
    p:fallbackToSystemLocale="false" />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
    up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<resources mapping="/favicon.ico" location="/favicon.ico" />

<beans:bean id="tilesConfigurer"
    class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <beans:property name="definitions">
        <beans:list>
            <beans:value>/WEB-INF/tiles-defs/templates.xml</beans:value>
            <beans:value>/WEB-INF/tiles-defs/definitions.xml</beans:value>
        </beans:list>
    </beans:property>
</beans:bean>

<beans:bean id="tilesViewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <beans:property name="viewClass"
        value="org.springframework.web.servlet.view.tiles3.TilesView" />
    <beans:property name="order" value="900" />
</beans:bean>

<beans:bean id="internalResourceViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
    <beans:property name="order" value="1000" />
</beans:bean>

<beans:bean id="validator"
    class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

这是我的网络流配置:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:webflow="http://www.springframework.org/schema/webflow-config"
    xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
           http://www.springframework.org/schema/webflow-config
           http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.4.xsd">


    <!-- Setup Web Flow here -->
    <webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
        <webflow:flow-location path="/WEB-INF/flows/form-flow.xml"
            id="eform_start" />
    </webflow:flow-registry>

    <webflow:flow-builder-services id="flowBuilderServices"
        view-factory-creator="mvcViewFactoryCreator" 
        />

    <bean id="mvcViewFactoryCreator"
        class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
        <property name="viewResolvers">
            <list>
                <ref bean="tilesViewResolver" />
            </list>
        </property>
    </bean>

    <webflow:flow-executor id="flowExecutor" />

    <!-- Enables FlowHandler URL mapping -->
    <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
        <property name="flowExecutor" ref="flowExecutor" />
    </bean>

    <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
        <property name="flowRegistry" ref="flowRegistry" />
        <property name="order" value="0" />
    </bean>

</beans>

这是我的注释,用于格式化日期:

@DateTimeFormat(pattern = "YYYY-MM-dd")
@NotNull
DateTime requestedDeadline;

yyyy-MM-dd也不起作用。

我清楚地以正确的格式提交了该值,但它无法正常工作。我做错了什么?

1 个答案:

答案 0 :(得分:1)

很遗憾,您还没有提供完整的上下文配置,但是您可以查看此块:

<annotation-driven />

我怀疑它缺少一个合适的命名空间(mvc:?),因此未加载默认转换。