Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Custom DataBinder in Spring 3.0

  1. #1
    Join Date
    Mar 2005
    Location
    Prague, Czech Republic
    Posts
    34

    Default Custom DataBinder in Spring 3.0

    Hello,

    I'm migrating our project to Spring 3.0. Most of the issues is resolved but one of them not.

    We used to create our own custom DataBinder in a AnnotationMethodHandlerAdapter subclass. We used to override the createBinder() method and created our own DataBinder implementation there.

    How to achieve the same in Spring 3.0. The createBinder() method disappeared.

    Thank you.

  2. #2
    Join Date
    Mar 2005
    Location
    Prague, Czech Republic
    Posts
    34

    Default

    Nobody with the same problem?

  3. #3
    Join Date
    Oct 2005
    Location
    Mobile, AL
    Posts
    345

    Default Here You Go

    You can use the following annotation declaration:

    Code:
    @InitBinder    
    public void initBinder(WebDataBinder binder) {
        ...define custom editors here.
    }

  4. #4
    Join Date
    Mar 2005
    Location
    Prague, Czech Republic
    Posts
    34

    Default

    Quote Originally Posted by MartyJones View Post
    You can use the following annotation declaration:

    Code:
    @InitBinder    
    public void initBinder(WebDataBinder binder) {
        ...define custom editors here.
    }
    You don't understand my needs. In Spring 2.5 we did something like this:
    Code:
    @InitBinder    
    public void initBinder(MyDataBinder binder) {
        ...define custom editors here.
    }
    And we were able to override createBinder() method in a Spring MVC class to produce our custom DataBinder. And it seems this is not possible in Spring 3.0.0.RC3

  5. #5
    Join Date
    Oct 2005
    Location
    Mobile, AL
    Posts
    345

    Default

    I understand your question now. Spring 3 has incorporated a ConversionService that does Type Conversion for you (at least the common conversions).

    They do state that if a ConversionService is not registered then it will fall back to the PropertyEditor method. The problem I have is that if you specifiy <mvc:annotation-driven /> then it by default loads the default conversion service so Property editor support does not work!

    Take a look at this link to learn how to set it up and how to add custom type converters:

    http://static.springsource.org/sprin...l/ch05s05.html
    Last edited by MartyJones; Dec 3rd, 2009 at 04:15 PM.

  6. #6
    Join Date
    Mar 2005
    Location
    Prague, Czech Republic
    Posts
    34

    Default

    Quote Originally Posted by MartyJones View Post
    I understand your question now. Spring 3 has incorporated a ConversionService that does Type Conversion for you (at least the common conversions).

    They do state that if a ConversionService is not registered then it will fall back to the PropertyEditor method. The problem I have is that if you specifiy <mvc:annotation-driven /> then it by default loads the default conversion service so Property editor support does not work!

    Take a look at this link to learn how to set it up and how to add custom type converters:

    http://static.springsource.org/sprin...l/ch05s05.html
    After reading the docs I still don't understand the problem. Even if I use ConversionService I still need to use @InitBinder to configure controller specific bindings.

    So how does ConversionService replace DataBinder? Especially in @InitBinder methods?

    Thanks.

  7. #7
    Join Date
    Oct 2005
    Location
    Mobile, AL
    Posts
    345

    Default

    Gary, You do NOT need to use initBinder if you are using the ConversionService. I just stood up a Web app that is using the Conversion service and I don't use @InitBinding.

    There are two basic conversion service factory beans.

    1. ConversionServiceFactoryBean - This class sets up default converters for converting strings to basic objects. This class allows you to specify custom object converters. For example:

    Code:
    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean" >
        <property name="converters">
            <list>
                <bean class="com.foo.MyClassConverter"/>
            </list>
        </property>
    </bean>
    2. FormattingConversionServiceFactoryBean - This class sets up basic formatters for doing date and number conversions as well as setup default converters for converting strings to objects.

    I extended the FormattingConversionServiceFactoryBean and added a setConverters method so that I can add my custom converters through the configuration.

    So to answer your question. You specify your custom type converters within the conversionService bean configuration setup like the code snippet above. The conversion service will handle the rest for you.

    Let me know if this did not answer your question.

    Marty

  8. #8
    Join Date
    Mar 2005
    Location
    Prague, Czech Republic
    Posts
    34

    Default

    Quote Originally Posted by MartyJones View Post
    Gary, You do NOT need to use initBinder if you are using the ConversionService. I just stood up a Web app that is using the Conversion service and I don't use @InitBinding.

    There are two basic conversion service factory beans.

    1. ConversionServiceFactoryBean - This class sets up default converters for converting strings to basic objects. This class allows you to specify custom object converters. For example:

    Code:
    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean" >
        <property name="converters">
            <list>
                <bean class="com.foo.MyClassConverter"/>
            </list>
        </property>
    </bean>
    2. FormattingConversionServiceFactoryBean - This class sets up basic formatters for doing date and number conversions as well as setup default converters for converting strings to objects.

    I extended the FormattingConversionServiceFactoryBean and added a setConverters method so that I can add my custom converters through the configuration.

    So to answer your question. You specify your custom type converters within the conversionService bean configuration setup like the code snippet above. The conversion service will handle the rest for you.

    Let me know if this did not answer your question.

    Marty
    I understand how ConversionService works. I can even imagine that I need less @InitBinder methods, but I don't believe that I will stop use them. There are still needs to specify custom bindings for most of the forms in our application.

    Anyway, this is not point of my issue! Have a look at DataBinder and WebDataBinder classes. There is a lot of methods designed to be overridden. For example getEmptyValue(), getAllowedFields(), etc. So you are allowed and encouraged to implement your own DataBinder if you need. And it was possible until Spring 3.0. So this issue hurts backwards compatibility too.

    I'm creating JIRA issue for this.

  9. #9
    Join Date
    Mar 2005
    Location
    Prague, Czech Republic
    Posts
    34

  10. #10
    Join Date
    Oct 2012
    Location
    Cluj-Napoca, RO
    Posts
    1

    Default

    Any ideea how i can do this (namely replace the WebDataBinder) for an annotated controller (@Controller) ?
    Thanks,
    Tudor

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •