Question

Je fais une validation du formulaire de printemps, mais je reçois:

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

Cependant, sous ma forme modelAttribute je:

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

Je pensais que le DateTimeFormat était responsable?

J'utilise la mise en veille prolongée-validateur 4.0.

Était-ce utile?

La solution

Theres une chance que vous devrez utiliser enregistrer un CustomDateEditor dans votre contrôleur (s) pour convertir une chaîne à une date. L'exemple ci-dessous dans la méthode va votre contrôleur, mais vous devrez changer le format de date pour tout ce que vous match de l'aide.


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

Autres conseils

Pour utiliser @DateTimeFormat vous devez installer FormattingConversionServiceFactoryBean. <mvc:annotation-driven> fait implicitement, mais si vous ne pouvez pas l'utiliser, vous besoin de quelque chose comme ceci:

<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>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top