Announcement Announcement Module
Collapse
No announcement yet.
Spring 2.0, pre-binding hook Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 2.0, pre-binding hook

    Hello, I am in desperate need to get control of a form before regular binding and validation occurs. A particular parameter in the request needs to be looked up using a DAO, then passed into the backing object such that all other request parameters are processed appropriately. I've tried using BaseCommandController but all of the really good methods seem to be marked FINAL. :-)

    There doesn't seem to be anything in the flow of SimpleFormController that I can exploit to get control pre-bind, only post-bind pre-validate. Any help is greatly appreciated, thanks!

  • #2
    Why not put the code in formBackingObject()? Just conditionalize whatever you want to do with isFormSubmission().

    Comment


    • #3
      Thanks for the reply. :-)

      I've gone back through the javadoc, and perhaps I'm misunderstanding the proper flow. The formBackingObject() is called after the user submits the form, but before data binding? You can think of the user interface as having a select drop down box which lets them set their office department. Depending on their selection for department, the rest of the form fields like payroll increase will have different binding results. I must process the department field first, and can do this easily outside of Spring MVC.

      The user interface is firmly in place, I can not change it to be a Wizard. If this would work, do you have any example code? I've been going through all the examples provided with the distribution but am not seeing any likely suspects. Thanks again!

      Comment


      • #4
        Originally posted by jieryn View Post
        The formBackingObject() is called after the user submits the form, but before data binding?
        Yes, of course - as you first need the object to bind to.

        Originally posted by jieryn View Post
        I must process the department field first, and can do this easily outside of Spring MVC.
        But I wonder if this is really the way to go. You will have to work with non-business-typed values (i.e. just plain strings from request parameters).

        Regards,
        Jörg

        Comment


        • #5
          Pseudocode for formBackingObject would look something like this:

          Code:
          public Object formBackingObject (HttpServletRequest request) {
           // get a default/new instance of the backing object
           Object retVal = super.formBackingObject(request);
           if(isFormSubmission()) {
            // you may want to load the object from the database here
            String departmentParameter = request.getParameter("department");
            MyObject obj = (MyObject) retVal; // cast
            // explicitly set the department field based on the value of the request parameter
            obj.setDepartment(some.converter(departmentParameter));
           }
           return retVal;
          }
          After this point, on form submit, binding would take place, and the department field would have been set already in this method. Does that make sense? Does it do what you want?

          Comment


          • #6
            YES! Thank you, that is definitely working for me now. I can now "force" an ordering of binding as it were.. you own! :-)

            One final problem, I hope, seems to be when using the SimpleFormController in this fashion. I populate the form with various drop downs, for example, the department codes. With initial GET, the form runs fine. When I hit the submit button for POST, the departments are missing and I take a NPE in the <form:select/> for departments.. erk. Any tips?

            Comment


            • #7
              I solved this by making sure I had the correct successView specified. I also made sure to override an onSubmit(...) variant instead of onSubmitAction; the onSubmit call makes a call to my overridden referencedData(...) to build up other model objects.

              Nice one!

              Comment


              • #8
                Glad to help!

                Comment


                • #9
                  I solved this by making sure I had the correct successView specified. I also made sure to override an onSubmit(...) variant instead of onSubmitAction; the onSubmit call makes a call to my overridden referencedData(...) to build up other model objects.
                  I had a similiar problem. The way I overcame this problem was using ServletContextAttributeExporter
                  Code:
                  <bean class="org.springframework.web.context.support.ServletContextAttributeExporter">
                    		<property name="attributes">
                      		<map>
                        			<entry key="rData">
                          			<ref bean="rData"/>
                        			</entry>
                      		</map>
                    		</property>
                  	</bean>
                  	
                  	<bean id="rData" class="ReferenceData" />
                  By doing this, spring will make your reference data available in the applicationScope. And you can just use things like
                  <spring:select items= ${applicationScope.rData.countries}.... />
                  But this solution is good only if you have static lists, like countries states..... If the department drop down is changing all the time this may not necessarily be a good option.

                  Comment

                  Working...
                  X