Announcement Announcement Module
No announcement yet.
formBackingObject called twice Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • formBackingObject called twice


    I am extending the AbstractWizardFormController and am running into the problem of the (overridden) formBackingObject method being called more than once in a wizard style form sequence. This leads to loss of data between POST requests.

    When I remove the formBackingObject implementation from my controller class the BaseCommandController.createCommand is executed twice in the form sequence leading to the same problem.

    I tried setting setSessionForm(true) in my controller, without effect.

    I am relying on Spring's build in support for setting the target page for page changes by specifying _target1, _target2 request parameters in my jsp's like this:

    <input class="button" name="_target1" type="submit" value="<fmt&#58;message key="" />">
    <input class="button" name="_cancel" type="submit" value="<fmt&#58;message key="addGallery.cancel" />">

    The problem first appeared when using this mechanism. Before, when relying on a implementation of the getTargetPage method, the problem did not occur.

    From other posts I concluded I should not override the getTargetPage method when relying on this mechanism, correct?

    I can provide code samples and debug level output if needed.

    Thanks for your help.

    -- Thomas

  • #2
    formBackingObject called twice

    Same problem here,

    The session form attribute seems to be getting lost on submitting the second page of our wizard. Thus handleRequestInternal calls handleInvalidSubmit which returns us to the first page of the wizard with a new command object.

    Any help would be appreciated.


    • #3
      Just to let people know that we've found a way around this (I'm working with paudi). We have to specifically set the session attribute in the wizard controller for the second page (i.e. in our referenceData() method, we have a
      session.setAttribute&#40;this.class.getName&#40;&#41;.form.<data holder name>, <object name>
      line that sets the attribute in the session. We were just under the impression that this should be done automatically by spring.


      • #4

        I'm basically NOT a GUI person but trying to develop a prototype and ended up the GUI using JSP, Spring MVC for that. I'm some experience with Struts.

        This question is not related to this post but as i see you guys using JSP/Spring MVC, i thought that i would post my question here.

        In my application, i've two objects Project and Resource which have many-to-many relationship. I've a Form for Project and another for Resource and if i try to add Project and resource, they work nicely. But in reality, i would like to add Resources to the Project. Hence i added a button caled "Add Project to Resource". This link takes me to the Resource edit form. The problem is that, i would like a way to tell the Resource form that i'm adding Resource to THIS Project so that it can add an entry in the association table called "ProjectResource".

        So, how will i indicate the Resource form that i'm adding a resource to THIS project. My idea is that we can save the Project as session attribute using

        request.getSession&#40;&#41;.setAttribute&#40;"Project", project&#41;;
        and retrieve the same attribute later. But i'm not clear exactly where should i put this code? I've the ProjectController and ProjectFormController class for Project form and similar ones for Resource. When the user clicks the "Add Resource to Project" button, could someone please let me know, how and where should i set this attribute? or is there any other better approach.

        If someone could point me to any references/samples, i can read it.

        I greatly appreciate any input on this.



        • #5
          I solved it with the following formBackingObject implementation:

           protected Object formBackingObject&#40;HttpServletRequest request&#41; throws Exception &#123;
                  Object command = null;
                      try &#123;
                          command = this.getCommand&#40;request&#41;;
                      &#125; catch &#40;Exception e&#41; &#123;
                ;"formBackingObject&#58; exception thrown " + e.getMessage&#40;&#41;&#41;;
                  if &#40;command == null&#41; &#123;
                      command = new Object&#40;&#41;;
                  WebUtils.setSessionAttribute&#40;request, getFormSessionAttributeName&#40;&#41;, command&#41;;
                  return command;
          Looking at the getCommand() implementation in the AbstractFormController leds me to believe that it is intentional that adding the command object to the session between form submissions should be done manually, but maybe I am still missing out on some essentials.


          • #6
            Of course "Object" in
            Object command = null;
            should be substituted with the Type of your command object, as it can be retrieved by the getCommandClass() method.


            • #7
              Thanks very much delnoij for the code sample.

              I'll try this out. Per my understanding, i need to set the 'Project' object in my session attribute in the ProjectFormController (is it onSubmit?). Is that correct?

              Thanks again!


              • #8
                Thanks very much delnoij.

                I implemented this based on your idea and it works great!

                Thanks a lot!


                • #9
                  In my Project Form, i would like to list all the "Resources", if any. Could you please point me to some references on how should i do that in JSP? (I'm looking for a drop-down menu). As i mentioned, i'm NOT a GUI person but can pick up things fast if someone can point out to references.

                  Thanks a lot!


                  • #10
                    In my ProjectDAO/Manager, i've a method that returns a set of "Resources".


                    • #11
                      In your controller implementation, you can override referenceData(HttpServletRequest request, int page):


                      protected Map referenceData(HttpServletRequest request, int page) {
                              Map model = new HashMap();
                              model.put("locationList", Location.getValues());
                               return model;
                      Here you could retrieve the Resource Set from your DAO.

                      I am using my domain object as form backing object as well; it has an attribute
                      private Location location

                      Then in your JSP, you would say:

                      <spring:bind path="command.location">
                      <select name="<c:out value="${status.expression}"/>">
                      <c:forEach var="location" items="${locationList}">
                      <option value="<c:out value="${location.code}"/>">
                      <c:out value="${}"/></option>
                      In your case, you might have to write a custom PropertyEditor for resolving the reference to the Resource Object. See:
                      Last edited by robyn; May 14th, 2006, 05:28 PM.


                      • #12
                        Thanks a lot Delnoij. I greatly appreciate your time in providing the sample code as well.


                        Thanks very much again!

                        I'll try this out and check the reference you provided as well.


                        • #13
                          And study the JPetstore example ...

                          public class OrderFormController extends AbstractWizardFormController


                          • #14
                            Sure delnoij. I'm using SimpleFormController for now. I did read about AbstractWizardFormController. I had gone through the backend code for PetClinic and JPetstore but ignored the front end code. That's how i learnt Spring. You are correct. I should take a look at the front end code now.

                            Thanks again!