Pregunta

Estoy haciendo un poco de validación de formularios primavera, sin embargo me estoy haciendo:

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

Sin embargo, en mi forma modelAttribute tengo:

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

pensé que el DateTimeFormat era responsable de esto?

Estoy usando la hibernación-validador 4.0.

¿Fue útil?

Solución

Hay un posibilidad de que usted tiene que usar registrar un CustomDateEditor en su controlador (s) para convertir de una cadena a una fecha. El método siguiente ejemplo va en su controlador, pero tendrá que cambiar el formato de fecha en el mismo importe que está utilizando.


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

Otros consejos

Para uso @DateTimeFormat es necesario instalar FormattingConversionServiceFactoryBean. <mvc:annotation-driven> lo hace de manera implícita, pero si no puede utilizarlo se necesita algo como esto:

<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>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top