How to avoid inserting two same records twice when double-clicking the submit button? [duplicate]

冷暖自知 提交于 2019-12-17 16:54:29

问题


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?


回答1:


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:


You can prevent the user from double clicking on the client side as suggested by @Stefan, but if you're looking for a way on preventing it from the backend/mysql check the answer from

Insert Where Not Exists-Without Primary Key



来源:https://stackoverflow.com/questions/28714011/how-to-avoid-inserting-two-same-records-twice-when-double-clicking-the-submit-bu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!