Avoid Duplicating Submit Struts 2 jsp Page

Hello to all. I have some jsp and im pages using struts2 to process my forms. After the user submits the form, the URL displayed in the address bar becomes somthing.action, so when the user refreshes the page, the forms are submitted again. How can I handle this? after submitting the form.

+7
source share
3 answers

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

If you just want to refresh the page after submitting without sending it again, redirect it to an action in which you show only the results. Use the result of redirectAction for this.

+4
source

+1 for both answers.

Post / Redirect / Get is a classic template for every web technology.

Token Interceptor is another way when you use Struts2;

There is a third way if you don't need retro compatibility with older browsers or browsers with Javascript disabled: HTML5 window.history.pushState .

Just reset the original URL after loading the page, and pressing F5 will get the original page instead of sending the request again.

  $(document).ready(function() { window.history.pushState("","", "myOriginalUrlWithNoParams"); }); 
+3
source

POST REDIRECT GET

This template must be followed to prevent the form from re-submitting when updating. This means that after sending the POST request, the POST must send a REDIRECT response to retrieve the landing page using GET . With this template, if the user refreshes the page, only the GET request appears again, so the same page is retrieved without updating on the server.

This is a general design template recommended for the web. Google will provide a lot of resources about this.

+1
source

All Articles