spring Spring中的重复表单提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2324931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:59:46  来源:igfitidea点击:

Duplicate form submission in Spring

formsspringspring-mvcform-submit

提问by Javi

What's the best way of avoiding duplicate form submission in Spring. Does this framework provide any special feature to handle this problem (for example as the Synchronizer Tokenin Struts)?

在 Spring 中避免重复表单提交的最佳方法是什么?这个框架是否提供任何特殊功能来处理这个问题(例如作为Struts 中的Synchronizer Token)?

回答by BalusC

There are different ways to avoid double submits, which can be combined:

避免双重提交的方法有多种,可以结合使用:

  1. Use JavaScript to disablethe button a few ms after click. This will avoid multiple submits being caused by impatient users clicking multiple times on the button.

  2. Send a redirect after submit, this is known as Post-Redirect-Get (PRG) pattern. This will avoid multiple submits being caused by users pressing F5 on the result page and ignoring the browser warning that the data will be resend, or navigating back and forth by browser back/forward buttons and ignoring the same warning.

  3. Generate an unique token when the page is requested and put in both the session scope and as hidden field of the form. During processing, check if the token is there and then remove it immediately from the session and continue processing. If the token is not there, then block processing. This will avoid the aforementioned kinds of problems.

  1. disable单击几毫秒后,将 JavaScript 用于按钮。这将避免由于不耐烦的用户多次点击按钮而导致多次提交。

  2. 提交后发送重定向,这称为Post-Redirect-Get (PRG) 模式。这将避免用户在结果页面上按 F5 并忽略浏览器警告数据将重新发送,或通过浏览器后退/前进按钮来回导航并忽略相同警告而导致多次提交。

  3. 在请求页面时生成一个唯一的令牌,并将其放入会话范围和表单的隐藏字段中。在处理过程中,检查令牌是否存在,然后立即将其从会话中删除并继续处理。如果令牌不存在,则阻止处理。这将避免上述类型的问题。

In Spring you can use RedirectViewas implementation of the PRG pattern (as described in point 2). The other two points needs to be implemented yourself.

在 Spring 中,您可以将其RedirectView用作 PRG 模式的实现(如第 2 点所述)。另外两点需要自己实现。

回答by bharal

this page seems to answer your question (for the token issue, I mean. The javascript and post-redirect-get parts of the question aren't covered here):

此页面似乎回答了您的问题(对于令牌问题,我的意思是。此处未涵盖问题的 javascript 和 post-redirect-get 部分):

http://explodingjava.blogspot.com/2009/03/spring-mvc-synchronizer-token.html

http://explodingjava.blogspot.com/2009/03/spring-mvc-synchronizer-token.html

回答by bpapa

Just do a redirect after post. After a form submission is successful, when returning your ModelAndView make sure the View is a RedirectView. From the user's POV, they submit the form, and then are redirected to make a "GET" to another URL. This way they won't double submit.

只需在发布后进行重定向。表单提交成功后,返回 ModelAndView 时,请确保 View 是 RedirectView。他们从用户的 POV 提交表单,然后被重定向以“GET”到另一个 URL。这样他们就不会重复提交。

Note that when using a Redirect View, Model Attributes get exposed in the URL as the parameters. So you might want to keep the attributes as thin as possible. What I typically do is show the user a page that doesn't really contain any unique information, just a "confirm" message.

请注意,当使用重定向视图时,模型属性在 URL 中作为参数公开。因此,您可能希望使属性尽可能地精简。我通常做的是向用户显示一个实际上不包含任何唯一信息的页面,只是一个“确认”消息。