View Full Version : Pass parameter to SimpleFormController
Akumadevil
Dec 5th, 2007, 01:53 AM
I have a SimpleFormController mapped to form.htm, but need to pass an id parameter to it from a standard non-form initial page.
First I tried "form.htm?id=100" but this does not work as when I go to submit the form it gives me the below error message (it sees the id field repeated twice - one in the url and one as a field on the form - and cannot convert a string array into a number):
Failed to convert property value of type [java.lang.String[]] to required type [long] for property id; nested exception is java.lang.NumberFormatException: For input string: "100,100"
So then I was told you should always POST to the SimpleFormController so altered my initial page to use POST as shown below:
<form name="preform" action="form.htm" method="post">
<input type="hidden" name="id" value="100" />
...
</form>
But what happens is when I submit the "preform" above it automatically submits "form.htm" as well (!) and I end up on the successView page.
What is the correct way to pass an id variable to form.htm?
The only solution I have found, which is less than ideal, is to use GET and change the parameter name (e.g. form.htm?load-id=100) which I can then parse in the formBackingObject.
Any help would be greatly appreciated.
ozosmail
Dec 5th, 2007, 07:21 AM
You want to submit a parameter to your SimpleControllerForm without submitting the form. Firstly, I think you should change the name of your first parameter so there are no conflicts. That is change
"form.htm?id=100"
To something like form.htm?sid=100.
You are going to access this parameter sid
protected Object formBackingObject(HttpServletRequest request) throws Exception {
String sid = request.getParameter("sid");
//Then if you have an interest in using this parameter in your form parameter you can do that here
}
Hope this helps
pmularien
Dec 5th, 2007, 09:06 AM
you should always POST to the SimpleFormController
This is true by default, but in cases where you want GET parameters bound to your command object, you can safely override isFormSubmission to alter the default behavior (and/or selectively override other superclass methods to get bind-on-GET behavior).
can then parse in the formBackingObject
This is a fine solution too, as long as you don't care about actually binding the values in the URL to your command object. Without seeing what exactly you're doing with this parameter in formBackingObject, it's hard to speculate.
Akumadevil
Dec 6th, 2007, 06:39 PM
Solution:
Use a basic POST form to pass information to the SimpleFormController page - except with 1 extra parameter to indicate these are initial values.
Initial page:
<form name=preform action=form method=post>
<input type=hidden name=load value=true />
<input type=hidden name=id value=100 />
...
</form>
SimpleFormController (form page):
Set "bindOnNewForm" to TRUE (I did this in the controller-servlet.xml)
Override isFormSubmission so when we travel to the form page it doesn't automatically submit.
@Override
protected boolean isFormSubmission(HttpServletRequest request) {
String load = request.getParameter("load");
if (load != null) {
return false;
}
else {
return super.isFormSubmission(request);
}
}
And that's it. I wish there was a way to do this using GET but there seems to be no way to get spring to bind only the POST parameters and ignore the GET ones.
pmularien
Dec 6th, 2007, 10:42 PM
to get spring to bind only the POST parameters and ignore the GET ones.
This is not specific to Spring - as per the Servlet specification, they are not differentiable. Quoting from the Servlet 2.4 spec:
When the request is an HttpServletRequest object,
and conditions set out in “When Parameters Are Available” on page 36 are met, the container populates the parameters from the URI query string and POST-ed data.
I do not believe it's possible to differentiate within the context of a Java servlet request whether a parameter came from the URL (GET) or as a POST parameter. This is generally desired, as other web frameworks that do differentiate (ColdFusion being the one that comes to mind first) can be very error-prone.
Hope that helps (although I confess that I really don't understand what problem you're trying to solve by doing this :) )...
Akumadevil
Dec 6th, 2007, 11:09 PM
I appreciate your reply.
I am trying to create a very custom form builder where a user starts with an (almost) empty page and proceeds to define input elements (i.e. text box, checkbox). When the user is finished they will click a button to export all the defined input elements to XML.
I have the initial form with a button "add new question" that loads a new window with a form (SimpleFormController) asking things like field name, length or value. This form contains about 20 questions.
Once the user adds a question I would like them to be able to edit it, which requires all the fields be pre-populated with the original answers (hence my problem).
I only want to persist the data at the very end because I should be able to pass parameters from the initial page to the SimpleFormController and back again.
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.