在Spring中提交重复的表单提交

编程入门 行业动态 更新时间:2024-10-28 05:28:38
本文介绍了在Spring中提交重复的表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在Spring中避免重复表单提交的最佳方法是什么?此框架是否提供了任何特殊功能来处理此问题(例如,在同步器令牌中Struts)?

解决方案

有两种不同的方法可以避免双重提交,可以合并:

  • 在点击后的几ms内,使用JavaScript来<禁用按钮。这将避免多次提交是由不耐烦的用户在按钮上多次点击引起的。

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

  • 请求页面时生成唯一标记,并将其放入会话作用域和表单的隐藏字段中。处理期间,检查令牌是否存在,然后立即将其从会话中删除并继续处理。如果令牌不存在,则阻止处理。这可以避免上述类型的问题。

  • code>作为PRG模式的实现(如第2点所述)。其他两点需要自己实施。

    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 Token in Struts)?

    解决方案

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

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

  • 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.

  • 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.

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

    更多推荐

    在Spring中提交重复的表单提交

    本文发布于:2023-10-07 17:53:29,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:表单   Spring

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!