Avoid duplicate submission of Struts 2 jsp page

2019-01-12 06:24发布

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.

3条回答
该账号已被封号
2楼-- · 2019-01-12 06:47

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.

查看更多
你好瞎i
3楼-- · 2019-01-12 06:55

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.

查看更多
地球回转人心会变
4楼-- · 2019-01-12 07:02

+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");
 });
查看更多
登录 后发表回答