スプリング検証:文字列から現在まで変換できません
-
29-09-2019 - |
質問
私はいくつかの春のフォームの検証をしていますが、私は取得しています:
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
ただし、私のモデルアトリブフォームには次のとおりです。
@NotNull
@Past
@DateTimeFormat(style="S-")
private Date birthdate;
DateTimeFormatはこれに責任があると思いましたか?
Hibernate-Validator 4.0を使用しています。
解決
レジスタを使用する必要がある可能性があります CustomDateEditor
コントローラーで、文字列から日付に変換します。以下の方法はコントローラーにありますが、使用しているものを一致させるために日付形式を変更する必要があります。
@InitBinder
public void initBinder(WebDataBinder binder) {
CustomDateEditor editor = new CustomDateEditor(new SimpleDateFormat("MM/dd/yyyy"), true);
binder.registerCustomEditor(Date.class, editor);
}
他のヒント
使用するために @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>
所属していません StackOverflow