[英]String Empty To Null - Empty Form Fields to null - Spring Editor - StringTrimmerEditor
我讀了幾篇文章,並嘗試以相同的方式實現,但是不知何故我遇到了錯誤。 我想通過XML based configuration
來實現。
兩種方式
基於注釋-工作正常
@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)
您必須指定一個類名作為值,而不是一個bean(如消息中所述),如下所示:
<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
是:
package your.package;
public class EmptyAsNullStringTrimmerEditor extends StringTrimmerEditor {
public YourClass() { super(true); }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.