I'm doing some spring form validation, however I'm getting:
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.开发者_Python百科String] to required typ
e [java.util.Date] for property 'birthdate': no matching editors or conversi
on strategy found
However, in my modelAttribute form I have:
@NotNull
@Past
@DateTimeFormat(style="S-")
private Date birthdate;
I thought the DateTimeFormat was responsible for this?
I'm using the hibernate-validator 4.0.
Theres a chance you'll have to use register a CustomDateEditor
in your controller(s) to convert from a String to a Date. The example method below goes in your controller, but you'll have to change the date format to match whatever you're using.
@InitBinder
public void initBinder(WebDataBinder binder) {
CustomDateEditor editor = new CustomDateEditor(new SimpleDateFormat("MM/dd/yyyy"), true);
binder.registerCustomEditor(Date.class, editor);
}
In order to use @DateTimeFormat
you need to install FormattingConversionServiceFactoryBean
. <mvc:annotation-driven>
does it implicitly, but if you cannot use it you need something like this:
<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>
精彩评论