Announcement Announcement Module
Collapse
No announcement yet.
Passing in initial values to a form controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Passing in initial values to a form controller

    What's the best design practice for passing initial values into a form? For example, let's say I have a page that contains a list of links that have the following URLs:

    foo.htm?id=1
    foo.htm?id=2
    foo.htm?id=3

    foo.htm is a page controlled by a class that overrides SimpleFormController. The idea is that foo.htm will present an empty form that includes a hidden form field with the ID from the URL above.

    What is the "Spring MVC" way to handle this type of interaction?

  • #2
    Override formBackingObject(HttpServletRequest) in your controller. Extract the parameter from the request, set the relevant field on your command object to this value and return the command object from here. Your view (JSP or whatever) can then create the hidden field by binding to the command object field.

    Code:
    <spring&#58;bind path="command.id">
      <input type="hidden" name="id" value="<c&#58;out value="$&#123;status.value&#125;"/>" />
    </spring&#58;bind>
    See http://www.springframework.org/docs/api/org/springframework/web/servlet/mvc/AbstractFormController.html#formBackingObject(java x.servlet.http.HttpServletRequest)

    Comment


    • #3
      If you have a formbacking object (command object) with a property id, it will automatically get populated from the request parameter. You can then refer to it in your view.

      So you hava a formbackingobject class:

      Code:
      public class FooCommand &#123;
          private String id;
      
          public void setId &#40;String id&#41; &#123;
              this.id = id;
          &#125;
      
          public String getId&#40;&#41; &#123;
               return id;
          &#125;
      
           etc.
      &#125;
      In the spring config for your controller you'll have:
      Code:
      <property name="commandName"><value>fooCmnd</value></property>
      <property name="commandClass"><value>org.xyz.FooCommand</value></property>
      and in your view, the command object will be available under the name fooCmnd. So if you're using jsp, your form can include a line like:
      Code:
      <input type="hidden" name="id" value="<c&#58;out value=$&#123;fooCmnd.id&#125;"/>">
      Hope this helps

      Comment


      • #4
        Just checked the javadoc - to bind request parameters to the command object on a form request, you need to set bindOnNewForm to true (default is false).

        So include in your controller config:
        <property name="bindOnNewForm"><value>true</value></property>

        Comment


        • #5
          good spot

          Comment


          • #6
            duplicate values for attribute

            I'm doing something similar, but I end up getting mutiple values back for the attributes I passed in. I'm guessing this is because the attribute is still in the URL that is used.

            How do I "strip" the url parameters once I've done what I need to do with them in the formBackingObject?

            Comment


            • #7
              That's a good question...I've never really found a canonical answer as to what most frameworks do when presented with a POST that also has query parameters; which one takes precedence?

              Within Spring, I've been using "foo.htm?_id=xxxx" and binding the values in formBackingObject myself, and spring doesn't try to set the "id" paramter.

              Comment


              • #8
                Thanks for the suggestion. That's a nice, simple solution. Why didn't I think of that? :oops:

                Comment


                • #9
                  No promises that it's the cleanest way...another option is to change the form action in the view from:

                  Code:
                  <form>
                  to

                  Code:
                  <form action="foo.htm">
                  The browser only uses the current URL if the action is empty...that adds the extra complication of the view needing to know it's URL, which I guess is just lopping off the query string in the controller and popping it in the model, for:

                  Code:
                  <form action="$&#123;formURL&#125;"
                  But I'm finding that I'm ending up with a bunch of such parameters that optionally pre-populate the command object anyway, so there's already a bunch of custom processing going on there. YMMV

                  Comment

                  Working...
                  X