View Full Version : Passing in initial values to a form controller
rafeco
Sep 21st, 2004, 03:38 PM
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?
davison
Sep 22nd, 2004, 02:46 AM
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:bind path="command.id">
<input type="hidden" name="id" value="<c:out value="${status.value}"/>" />
</spring:bind>
See http://www.springframework.org/docs/api/org/springframework/web/servlet/mvc/AbstractFormController.html#formBackingObject(java x.servlet.http.HttpServletRequest)
cmgharris
Sep 22nd, 2004, 02:49 AM
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 {
private String id;
public void setId (String id) {
this.id = id;
}
public String getId() {
return id;
}
etc.
}
In the spring config for your controller you'll have:
<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:
<input type="hidden" name="id" value="<c:out value=${fooCmnd.id}"/>">
Hope this helps
cmgharris
Sep 22nd, 2004, 03:00 AM
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>
davison
Sep 22nd, 2004, 03:11 AM
good spot :)
joeldavis
Jan 13th, 2005, 03:25 PM
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?
rob_from_ca
Jan 17th, 2005, 09:51 AM
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.
joeldavis
Jan 17th, 2005, 10:05 AM
Thanks for the suggestion. That's a nice, simple solution. Why didn't I think of that? :oops:
rob_from_ca
Jan 17th, 2005, 10:14 AM
No promises that it's the cleanest way...another option is to change the form action in the view from:
<form>
to
<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="${formURL}"
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
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.