Announcement Announcement Module
Collapse
No announcement yet.
Custom DataBinder in Spring 3.0 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Nobody with the same problem?

    Comment


    • #3
      Here You Go

      You can use the following annotation declaration:

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

      Comment


      • #4
        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

        Comment


        • #5
          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, 05:15 PM.

          Comment


          • #6
            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.

            Comment


            • #7
              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

              Comment


              • #8
                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.

                Comment


                • #9
                  JIRA issue SPR-6534

                  http://jira.springframework.org/browse/SPR-6534

                  Comment


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

                    Comment


                    • #11
                      Originally posted by tudor.stanciu View Post
                      Any ideea how i can do this (namely replace the WebDataBinder) for an annotated controller (@Controller) ?
                      Thanks,
                      Tudor
                      I need to do the same thing. I want to use a custom Errors implementation, but the Errors implementation used is instantiated by the DataBinder (always BeanPropertyBindingResult or DirectFieldBindingResult as far as I can tell). So in order to use a custom Errors implementation I need to use and implement a custom DataBinder.

                      Anyone know if this is even possible?
                      Last edited by ndtreviv; Apr 2nd, 2013, 04:44 AM. Reason: corrected misinformation

                      Comment


                      • #12
                        Hi,
                        I've read through all of the Spring 3 Web but have been completely unable to find any interesting documentation on binding more complicated request data, for example, let's say I use jQuery to post to a controller.

                        Comment


                        • #13
                          Originally posted by Rizulliam View Post
                          Hi,
                          I've read through all of the Spring 3 Web but have been completely unable to find any interesting documentation on binding more complicated request data, for example, let's say I use jQuery to post to a controller.
                          Tried this: http://static.springsource.org/sprin...tml#validation ?

                          It's important to distinguish between a DataBinder and a Converter/PropertyEditor. A DataBinder USES Converters/PropertyEditors to convert submitted request data into the type of object you need.

                          What I actually needed was a two-step process. I needed to convert a string to a user, for example, and then have that bound to an object that describes that user field entry, rather than the member that IS that user field entry.
                          For this, I needed to write a custom binder, which even though I did I discovered I couldn't use it because of the way that Spring's argument resolution works.
                          HOWEVER, I did manage to get around it by writing my own argument resolver which was responsible for binding values to the object in the way I needed it to, and produced a custom errors implementation as well.
                          Sorted for me.

                          Comment

                          Working...
                          X