如何避免插入2相同的记录时,两次双击提交按钮? [英] How to avoid inserting two same records twice when double-clicking the submit button?
问题描述
我有一个弹出式窗口,其中有两个的onclick方法为提交和放弃。
当我点击提交这两次插入两个记录,这意味着重复记录。
我怎样才能避免这种情况?
preventing双提交是一个常见的问题。
虽然有很多的解决方案,它,因为你已经标记它的 Struts2的,有框架提供了即装即用两种方式来处理双提交:
-
的令牌拦截
它返回一个
invalid.token
结果是可以,例如,被映射到一个错误:确保每个令牌只有一个请求被处理。这个拦截器可以确保后退按钮和双击不会导致非预期的侧面影响。例如,您可以用它来prevent粗心的用户谁可能双击一个结账在一个在线商店按钮。该拦截器使用一个相当原始的技术,当一个无效的令牌发现:它返回的结果
invalid.token
,它可以映射到您的操作配置。更为复杂的执行,TokenSessionStoreInterceptor,可以提供被发现无效标记时,为更好的逻辑。注意:要设置令牌表单,你应该使用
标记标记
。此标记是必需的,必须在提交给该拦截器保护行动的形式使用。不提供一个令牌(使用令牌标记)任何请求将被处理为一个无效的令牌的请求。 -
的令牌会话拦截
它建立关令牌拦截器,但有一个更先进,更人性化的行为,即正是你需要:对于双形式提交,它呈现的第一个结果,有效的请求,而默默吞下第二个(和后续)请求(S):
该拦截器建立关TokenInterceptor,对于处理无效令牌提供先进的逻辑。不同于通常的道理拦截器,这个拦截器将尝试提供智能的故障转移在使用同一个会话的多个请求的情况下。也就是说,它会阻止,而不是返回
invalid.token
code后面的请求,直到第一个请求完成,然后,它会尝试显示同样的反应,如果没有多个请求提交摆在首位,原来,有效的操作调用会显示出来。
重要!
值得一提的是,完全一样的验证,像这样的服务器端解决方案应该是<强>强制,而像禁止提交使用Javascript按钮单独一个客户端解决方案是不够的。
如果用户重新启用与萤火虫的按钮?如果他/她伪造使用Javascript的要求?如果我对我的银行网站,尝试发送一个请求两次(如现金转移),这是至关重要的,它得到处理一次。
然后与服务器端解决方案去,如果你真的想的,也添加客户端保护......牢记,你需要仔细检查每一个可能的情况下,不最终在你的页面中被禁用按钮后,请求结束(尤其是的 AJAX ,你的标签)由于不可预见的结果。
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.
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:
The Token Interceptor
It returns an
invalid.token
result that can, for example, be mapped to an error: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.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):
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.
IMPORTANT !
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.
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.
这篇关于如何避免插入2相同的记录时,两次双击提交按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!