2014-12-09 3 views
0

나는 약간의 게시물을 읽었으며 같은 방법으로 구현하려했지만 어쨌든 나는 아래 오류가 발생했다. XML based configuration을 통해 구현하고 싶습니다.문자열 공백 Null - 비어있는 양식 필드를 null로 - 스프링 편집기 - StringTrimmerEditor

두 가지 방법

주석 기반은 - 기반 미세

@ControllerAdvice 
@Controller 
public class AppBindingInitializer { 

    @InitBinder 
    public void initBinder(WebDataBinder binder) { 
     binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); 
    } 

} 

XML 작품 - 가져 오기 오류

<bean id="coreCustomEditors" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> 
    <property name="customEditors"> 
     <map> 
      <entry key="java.lang.String"> 
       <bean class="org.springframework.beans.propertyeditors.StringTrimmerEditor"> 
        <constructor-arg name="emptyAsNull" value="true" /> 
       </bean> 
      </entry> 
     </map> 
    </property> 
</bean> 

예외

Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.util.LinkedHashMap' to required type 'java.util.Map' for property 'customEditors'; nested exc 
eption is java.lang.IllegalArgumentException: Cannot convert value of type [org.springframework.beans.propertyeditors.StringTrimmerEditor] to required type [java.lang.Class] for property 'customEditor 
s[java.lang.String]': PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value of type [org.springframework.beans.propertyeditors.StringTrimmerEditor] 
     at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:479) 
     at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:511) 
     at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:505) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1502) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1461) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 
     ... 53 more 
Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [org.springframework.beans.propertyeditors.StringTrimmerEditor] to required type [java.lang.Class] for property 'customEdito 
rs[java.lang.String]': PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value of type [org.springframework.beans.propertyeditors.StringTrimmerEditor] 
     at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:263) 
     at org.springframework.beans.TypeConverterDelegate.convertToTypedMap(TypeConverterDelegate.java:623) 
     at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:208) 
     at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:459) 

답변

0

이 같은 값 (메시지에 의해 명시된 바와 같이)이 아니라 빈으로 클래스 이름을 지정해야합니다 :

package your.package; 

public class EmptyAsNullStringTrimmerEditor extends StringTrimmerEditor { 
    public YourClass() { super(true); } 
} 

CustomEditorConfigurer javadoc

를 참조하십시오

<bean id="coreCustomEditors" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> 
    <property name="customEditors"> 
     <map> 
      <entry key="java.lang.String" value="your.package.EmptyAsNullStringTrimmerEditor"/> 
     </map> 
    </property> 
</bean> 

존재 EmptyAsNullStringTrimmerEditor

+0

'' –

+0

두 번째 옵션에서 i와는 달리 어떤 유형의 데이터 유형에 대한 편집기를 식별합니까? n 우리가'key as java.lanag.String '과 그 값을 에디터로 제공한다. –

+0

미안하다. 문서를 잘못 읽었을 때, 서브 클래 싱을해야한다. 내 편집을 참조한다. –