4

私はいくつかの春のフォーム検証を行っていますが、私は得ています:

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 を使用しています。

4

2 に答える 2

10

CustomDateEditorString から Date に変換するために、コントローラーでregister a を使用する必要がある場合があります。以下のメソッドの例はコントローラに適用されますが、使用しているものに合わせて日付形式を変更する必要があります。


@InitBinder
    public void initBinder(WebDataBinder binder) {
        CustomDateEditor editor = new CustomDateEditor(new SimpleDateFormat("MM/dd/yyyy"), true);
        binder.registerCustomEditor(Date.class, editor);
    }
于 2010-11-29T11:15:50.203 に答える
4

使用@DateTimeFormatするには、インストールする必要がありますFormattingConversionServiceFactoryBean<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>
于 2010-11-29T11:41:01.833 に答える