[英]String Empty To Null - Empty Form Fields to null - Spring Editor - StringTrimmerEditor
I have read few posts, and tried implementing the same way, but somehow i am getting below error. 我读了几篇文章,并尝试以相同的方式实现,但是不知何故我遇到了错误。 I want to implement through
XML based configuration
. 我想通过
XML based configuration
来实现。
Two Ways 两种方式
Annotation Based - Works Fine 基于注释-工作正常
@ControllerAdvice
@Controller
public class AppBindingInitializer {
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
}
XML Based - Getting Error 基于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>
Exception 例外
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)
You have to specify a class name as value, not a bean (as stated by the message), like this: 您必须指定一个类名作为值,而不是一个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>
with EmptyAsNullStringTrimmerEditor
being: 与
EmptyAsNullStringTrimmerEditor
是:
package your.package;
public class EmptyAsNullStringTrimmerEditor extends StringTrimmerEditor {
public YourClass() { super(true); }
}
see CustomEditorConfigurer
javadoc 参见
CustomEditorConfigurer
javadoc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.