2010-11-29 3 views
3

日までの文字列から変換することはできません。春の検証:しかし、私は取得しています、私はいくつかの春のフォーム検証をやってる

Failed to convert property value of type 'java.lang.String' to required type 'ja 
va.util.Date' for property 'birthdate'; nested exception is java.lang.Illega 
lStateException: Cannot convert value of type [java.lang.String] to required typ 
e [java.util.Date] for property 'birthdate': no matching editors or conversi 
on strategy found 

しかし、私のmodelAttributeの形で私が持っている:

@NotNull 
@Past 
@DateTimeFormat(style="S-") 
private Date birthdate; 

DateTimeFormatがこれを担当していると思いましたか?

私はhibernate-validator 4.0を使用しています。

+0

''はありますか? – axtavt

+0

これはポートレットにあり、mvc:annotation-drivenは機能しません。私が使用します。 \t \t <豆ID = "annotationMethodHandlerAdapter" クラス= "org.springframework.web.portlet.mvc.annotation .AnnotationMethodHandlerAdapter "> \t <プロパティ名=" webBindingInitializer "> \t <ビーンID =" configurableWebBindingInitializer」クラス= "org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> \t <プロパティ名= "バリ"> \t jack

+0

\t \t \t – jack

答えて

9

Theresあなたのコントローラで登録するCustomDateEditorを使用して、文字列から日付に変換する必要があります。以下のサンプルメソッドはあなたのコントローラにありますが、使用しているものに合わせて日付フォーマットを変更する必要があります。あなたがFormattingConversionServiceFactoryBeanをインストールする必要が@DateTimeFormat使用するためには


@InitBinder 
    public void initBinder(WebDataBinder binder) { 
     CustomDateEditor editor = new CustomDateEditor(new SimpleDateFormat("MM/dd/yyyy"), true); 
     binder.registerCustomEditor(Date.class, editor); 
    } 
4

<mvc:annotation-driven>これは暗黙のことですが、使用できない場合は次のようなものが必要です。

<bean id="conversionService" 
    class="org.springframework.format.support.FormattingConversionServiceFactoryBean" /> 

<bean id="annotationMethodHandlerAdapter"  
    class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
    <property name="webBindingInitializer"> 
     <bean id="configurableWebBindingInitializer" 
      class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> 
      <property name="validator"><ref bean="validator"/> 
      <proeprty name = "conversionService" ref = "conversionService" /> 
     </bean> 
    </property> 
</bean>