Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid duplicate submission of Struts 2 jsp page

Hi all I've got some jsp pages and im using struts2 to handle my forms. After submitting a form by user, the url shown in address bar becomes somthing.action, so when the user refreshes the page, the forms gets submitted again. How can I handle this? after submission of a form.

like image 981
edaklij Avatar asked Nov 23 '12 12:11

edaklij


3 Answers

If the goal is to prevent duplicate submission of forms then use token interceptor http://struts.apache.org/2.x/docs/token-interceptor.html or tokenSession interceptor http://struts.apache.org/2.x/docs/token-session-interceptor.html.

If you simple want to refresh the page after submit without submitting again then redirect to action where you only show results not form. Use redirectAction result for that.

like image 93
Aleksandr M Avatar answered Nov 10 '22 08:11

Aleksandr M


+1 to both the other answers.

Post/Redirect/Get is the classic Pattern for every web technology.

Token Interceptor is another way to go, when you are using Struts2;

There is a third way to go, if you don't care about retro-compatibility with old browsers, or browsers with Javascript disabled: HTML5's window.history.pushState.

Just reset the url to the original one after the page is loaded, and pressing F5 will get the original page, instead of re-submitting the request.

 $(document).ready(function() {
   window.history.pushState("","", "myOriginalUrlWithNoParams");
 });
like image 37
Andrea Ligios Avatar answered Nov 10 '22 06:11

Andrea Ligios


POST REDIRECT GET

This pattern needs to be followed to prevent re-submission of form on refresh. This means, after submitting a POST request, POST should send a REDIRECT response to fetch the destination page using GET. With this pattern, if the user refreshes the page, only the GET request happens again, so the same page is fetched without updating anything in server.

This is a common design pattern recommended for web. Google would provide a lot of resources about this.

like image 25
Arjun Avatar answered Nov 10 '22 08:11

Arjun