Domanda

che sto facendo un po 'di validazione dei form primavera, però mi sto:

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

Tuttavia, nella mia forma modelAttribute ho:

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

ho pensato che il DateTimeFormat era responsabile di questo?

sto usando il hibernate-validatore 4.0.

È stato utile?

Soluzione

C'è una possibilità dovrete usare registrare un CustomDateEditor nel controllore (s) per la conversione da una stringa in una data. Il metodo esempio che segue va nel vostro controller, ma si dovrà cambiare il formato della data per l'importo da te sta utilizzando.


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

Altri suggerimenti

Per poter utilizzare @DateTimeFormat è necessario installare FormattingConversionServiceFactoryBean. <mvc:annotation-driven> lo fa implicitamente, ma se non è possibile utilizzarlo avete bisogno di qualcosa di simile a questo:

<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>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top