双击提交按钮时如何避免两次插入两条相同的记录?

How to avoid inserting two same records twice when double-clicking the submit button?(双击提交按钮时如何避免两次插入两条相同的记录?)

本文介绍了双击提交按钮时如何避免两次插入两条相同的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个弹出窗口,其中有两种 onclick 方法,即提交"和放弃".

I have a popup window where there are two onclick methods as "Submit" and "Discard".

当我点击 提交 两次时,它会插入两条记录,这意味着重复记录.

When I click submit twice it insert two records that means duplicate record.

我怎样才能避免这种情况?

How can I avoid this?

推荐答案

防止重复提交是一个常见问题.

Preventing a double-submit is a common problem.

尽管有很多解决方案,因为你已经标记了它 struts2,框架提供了两种开箱即用的方式来处理双重提交:

Even though there are many solutions to it, since you've tagged it struts2, there are two ways provided out-of-the-box by the framework to handle double-submit:

  • 令牌拦截器

    它返回一个 invalid.token 结果,例如可以映射到错误:

  • The Token Interceptor

    It returns an invalid.token result that can, for example, be mapped to an error:

确保每个令牌只处理一个请求.此拦截器可以确保后退按钮和双击不会导致意外的副作用.例如,您可以使用它来防止粗心的用户可能会双击结帐"按钮在线商店.当发现无效令牌时,此拦截器使用一种相当原始的技术:它返回结果 invalid.token,该结果可以映射到您的操作配置中.更复杂的实现,TokenSessionStoreInterceptor,可以为找到无效令牌提供更好的逻辑.

Ensures that only one request per token is processed. This interceptor can make sure that back buttons and double clicks don't cause un-intended side affects. For example, you can use this to prevent careless users who might double click on a "checkout" button at an online store. This interceptor uses a fairly primitive technique for when an invalid token is found: it returns the result invalid.token, which can be mapped in your action configuration. A more complex implementation, TokenSessionStoreInterceptor, can provide much better logic for when invalid tokens are found.

注意:要在表单中设置令牌,您应该使用 token 标记.此标记是必需的,并且必须在提交到受此拦截器保护的操作的表单中使用.任何不提供令牌(使用令牌标签)的请求都将被视为带有无效令牌的请求.

Note: To set a token in your form, you should use the token tag. This tag is required and must be used in the forms that submit to actions protected by this interceptor. Any request that does not provide a token (using the token tag) will be processed as a request with an invalid token.

  • 令牌会话拦截器

    它建立在令牌拦截器的基础上,但具有更高级和用户友好的行为,正是您所需要的:在双重表单提交的情况下,它呈现第一个的结果, 有效的请求,同时默默地吞下第二个(和后续的)请求:

  • The Token Session Interceptor

    It builds off the Token Interceptor, but with a more advanced and user-friendly behaviour, that is exactly what you need: in case of a double form submit, it renders the result of the first, valid request, while silently swallowing the second (and the subsequent) request(s):

    这个拦截器建立在 TokenInterceptor 之上,提供了处理无效令牌的高级逻辑.与普通令牌拦截器不同,此拦截器将尝试在使用同一会话的多个请求的情况下提供智能故障转移.也就是说,它将阻止后续请求,直到第一个请求完成,然后它不会返回 invalid.token 代码,而是尝试显示与原始有效操作相同的响应如果一开始没有提交多个请求,则会显示调用.

    This interceptor builds off of the TokenInterceptor, providing advanced logic for handling invalid tokens. Unlike the normal token interceptor, this interceptor will attempt to provide intelligent fail-over in the event of multiple requests using the same session. That is, it will block subsequent requests until the first request is complete, and then instead of returning the invalid.token code, it will attempt to display the same response that the original, valid action invocation would have displayed if no multiple requests were submitted in the first place.

  • 值得注意的是,与验证完全相同,这样的服务器端解决方案应该是强制,而仅使用 Javascript 禁用提交按钮这样的客户端解决方案是不够的.
    如果用户使用 Firebug 重新启用按钮会怎样?如果他/她使用 Javascript 伪造请求怎么办?如果我在我的银行网站上并尝试发送两次请求(例如现金转账),那么它只被处理一次是至关重要的.

    It is worth noting that, exactly like for the Validation, a server-side solution like this should be mandatory, while a client-side solution like disabling the submit button with Javascript alone is not enough.
    What if the user re-enables the button with Firebug ? What if he/she forges a request with Javascript ? If I'm on my Bank site and try to send a request twice (eg. a cash transfer), it is crucial that it gets processed only once.

    然后使用服务器端解决方案,如果你真的想要,也可以添加客户端保护......请记住,你需要仔细检查每一种可能的情况,而不是请求结束后在您的页面中显示禁用按钮(尤其是 ajax,您标记的) 由于不可预见的结果.

    Then go with the server-side solution, and if you really want, add a client-side protection too... keeping in mind that you need to carefully check every possible case, to not end up in your page with a disabled button after a request is over (especially with ajax, that you tagged) due to an unforeseen result.

    这篇关于双击提交按钮时如何避免两次插入两条相同的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本文标题为:双击提交按钮时如何避免两次插入两条相同的记录?

    基础教程推荐