請求方法的參數我有一個模型類接受的DateTime(Jodatime),如用SpringMVC
class SomeClass {
private DateTime myDate;
// setter & getter
}
以及控制器:
@Controller
class MyController {
@RequestMapping("...")
public String doStuff(@ModelAttribute("myAttribute") SomeClass value) {
// ...
}
}
當調用從一個HTML5形式此控制器與適當<input type="datetime" .../>
字段我收到以下錯誤:
Field error in object 'myAttribute' on field 'myDate': rejected value [2014-02-08T23:00:00.00Z]; codes [typeMismatch.myAttribute.myDate,typeMismatch.myDate,typeMismatch.org.joda.time.DateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [myAttribute.myDate,myDate]; arguments []; default message [myDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'org.joda.time.DateTime' for property 'myDate'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.joda.time.DateTime] for property 'myDate': no matching editors or conversion strategy found]
這讓我感到困惑,因爲當我rstand在7.6.5 Configuring Formatting in Spring MVC
的manual:
Full support for the Joda Time formatting library is also installed if Joda Time is present on the classpath.
轉換應該只是工作開箱(我用彈簧4.0和喬達時間2.3)的。至少它應該找到一個轉換器,即使格式可能是錯誤的。不過,我認爲應該嘗試配置日期格式,如7.7 Configuring a global date & time format
中所述。從谷歌一些額外的幫助,關於HTML5日期字段和Javadoc的格式,我想出了這一點:
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatterRegistrars">
<set>
<bean class="org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar">
<property name="useIsoFormat" value="true" />
</bean>
</set>
</property>
</bean>
但是我剛剛插進我的applicationContext.xml這些行的效果也沒有給任何效果。
那麼我需要做什麼才能正確設置(另外我想避免在我的模型對象上註釋@DateTimeFormat
,因爲我的模型不知道關於HTML的任何內容,所以我感覺錯誤的是附加信息關於該對象上的HTML5格式)。
嗯...你是對的。當我閱讀手冊時,我忽略了這個選項,因爲我認爲我所做的與註釋無關。無論春季消息來源的人在計劃這個功能時都會這麼想。 – yankee