Announcement Announcement Module
No announcement yet.
Passing in initial values to a form controller Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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 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.

    <spring&#58;bind path="">
      <input type="hidden" name="id" value="<c&#58;out value="$&#123;status.value&#125;"/>" />
    See x.servlet.http.HttpServletRequest)


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

      public class FooCommand &#123;
          private String id;
          public void setId &#40;String id&#41; &#123;
     = id;
          public String getId&#40;&#41; &#123;
               return id;
      In the spring config for your controller you'll have:
      <property name="commandName"><value>fooCmnd</value></property>
      <property name="commandClass"><value></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:
      <input type="hidden" name="id" value="<c&#58;out value=$&#123;;"/>">
      Hope this helps


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


        • #5
          good spot


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


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


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


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


                  <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:

                  <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