wood burning stoves 2.0*
The moose likes Spring and the fly likes F5 or page refresh calls onSubmit of spring controller - database again updated Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "F5 or page refresh calls onSubmit of spring controller - database again updated" Watch "F5 or page refresh calls onSubmit of spring controller - database again updated" New topic
Author

F5 or page refresh calls onSubmit of spring controller - database again updated

Kavita Shivani
Ranch Hand

Joined: Aug 14, 2009
Posts: 45
Hi All,
I do a form submit in my first page, the form data is saved and returned in the next page as a result set. Now when I refresh the second page, the data is again saved in the database from the previous page. I could see that the onSubmit of the first page controller is being called again. How do I stop this. Because I already saved the data and I also have the result set in my command object on refresh i simply want to display the existing values.

thank you,
Kavitha.
James Ward
Ranch Hand

Joined: Apr 27, 2003
Posts: 263
You can generate a Unique Id and store in a hidden field in the form on the first page.
When you save the data, save this unique Id too.

Before saving, always check for presence of Unique Id, if it exists, it means this is a Re-Submit of the same information!
Kavita Shivani
Ranch Hand

Joined: Aug 14, 2009
Posts: 45
Do I create this field in the database and save it the first time a record is saved?
Kavita Shivani
Ranch Hand

Joined: Aug 14, 2009
Posts: 45
It would be good if we have some method in the controller class kind of onRefresh() which we could override and call onsubmit if necessary or just load the existing data again.
James Ward
Ranch Hand

Joined: Apr 27, 2003
Posts: 263
Kavita Shivani wrote:Do I create this field in the database and save it the first time a record is saved?

Sure.
Amit kull
Ranch Hand

Joined: Jun 05, 2008
Posts: 46
Hi,
Have a look at Point 4.6 of Spring MVC Step By Step tutorial here
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
You should redirect after submit, so user can refresh the page without calling onSubmit again.


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Kavita Shivani
Ranch Hand

Joined: Aug 14, 2009
Posts: 45
Ok that worked great for me. Since its a dumb page after form submission, I dint have much to do, I did a redirect, and in the next page's controller I have overridden referenceData() method of the spring's SimpleFormController class which gets called repeatedly (no additonal code necessary here for this to work) but to let you know that this gets called on every refresh. I make sure I send the request attibutes that I need in my next page at the end of the onsubmit. And its working fine.

To help anyone who will search for this in future I have placed my code snippet.
In my page one on form submit:


In my page 2 do nothing, just F5 and you still have old data and no submit action takes place for the old page.

Thank you, everyone for your response and suggestion.

Cheers,
Kavita.
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Thanks for the update. Submit-Redirect is a pattern to specifically solve this problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: F5 or page refresh calls onSubmit of spring controller - database again updated