簡體   English   中英

具有@DateTimeFormat批注的Spring MVC JodaTime轉換

[英]Spring MVC JodaTime conversion with @DateTimeFormat annotation

我有一個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" />

這是我的webflow配置:

<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也不起作用。

我顯然以正確的格式提交了值,但是它不起作用。 我究竟做錯了什么?

不幸的是,您尚未提供完整的上下文配置,但是可以檢查以下代碼段:

<annotation-driven />

我懷疑它缺少適當的名稱空間( mvc: ),因此未加載默認轉換。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM