2010-11-29 4 views
3

최신으로 문자열로 변환 할 수 없습니다 :봄 검증 : 그러나 내가지고있어, 좀 봄 양식 유효성 검사를하고 있어요

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 

하지만, 내 ModelAttribute 모 데르 형태로 내가 가진 :

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

DateTimeFormat이 이에 대한 책임이 있다고 생각합니까?

최대 절전 모드 유효성 검사기 4.0을 사용하고 있습니다.

+0

' '이 있습니까? – axtavt

+0

이것은 포틀릿에 있으므로 mvc : annotation-driven가 작동하지 않는다는 것을 잊어 버리십시오. 내가 사용하는 : \t \t \t <속성 이름 ="webBindingInitializer "> \t <콩 ID ="configurableWebBindingInitializer "클래스 ="org.springframework.web.bind.support.ConfigurableWebBindingInitializer "> \t <속성 이름 ="검증 "> \t jack

+0

\t \t \t – jack

답변

9

귀하의 컨트롤러에 CustomDateEditor을 등록해야만 String을 Date로 변환 할 수 있습니다. 아래의 예제 메서드는 컨트롤러에서 사용되지만 사용하는 모든 형식과 일치하도록 날짜 형식을 변경해야합니다. 당신은 FormattingConversionServiceFactoryBean를 설치해야 @DateTimeFormat 사용하기 위해서는


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

. <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>