【问题标题】:String Empty To Null - Empty Form Fields to null - Spring Editor - StringTrimmerEditorString Empty To Null - 将表单字段清空为 null - Spring Editor - StringTrimmerEditor
【发布时间】:2014-12-09 06:51:46
【问题描述】:

我已经阅读了几篇文章,并尝试以相同的方式实现,但不知何故我遇到了错误。我想通过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)

【问题讨论】:

    标签: java spring spring-mvc property-editor


    【解决方案1】:

    您必须将类名指定为值,而不是 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); }
    }
    

    CustomEditorConfigurer javadoc

    【讨论】:

    • 怎么传&lt;constructor-arg name="emptyAsNull" value="true" /&gt;
    • 在第二个选项中,它如何识别哪个编辑器用于哪种类型的数据类型,与第一个选项不同的是,我们提供 `key as java.lanag.String' 和 value 作为其编辑器
    • 对不起,我看错了文档,你必须在这里子类化,看我的编辑
    • 我试过你的方法,做了所有可能的改变,添加依赖等,但似乎是通过 xml 做的,它甚至没有注册我的 bean。但如果我按照我的第一种方法似乎可以正常工作。
    猜你喜欢
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    • 2020-09-24
    • 2016-02-07
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 2019-11-27
    相关资源
    最近更新 更多