Wednesday, August 13, 2014

CustomEditorConfigurer does not works with Form Data Binding in Spring

Spring comes with type conversion capabilities for many simple types and other straightforward types. However if we want to support user defined types or other types, we need to extend PropertyEditorSupport and implement the setAsText and getAsText method. Than we need to register our new type in Spring configuration XML as follows

<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
   <property name="customEditors">
        <entry key=<Type we want to handle> value=<Custom Editor>/>

However this conversion does not works with form data binding framework. That means if we have a code similar to as follows:

<form:input id="id1" path="customType"/>

Where the custom Type is the type that we expect to go through our Custom editor. Unfortunately it does not works. For it to work, we have to register it explicitly. For example in the controller class, register a method with InitBinder

    protected void initBinder(HttpServletRequest request, 
                             ServletRequestDataBinder binder){
                (<Type we want to handle through custom Editor>,
                 new <Custom_Editor>));    

More Articles on Spring

No comments:

Post a Comment