简体   繁体   English

JSF提交按钮 - 表单在一秒钟内被提交X次(直到视图重新创建) - 如何防止它?

[英]JSF submit button - form is X times submited during one second (until view recreated) - how to prevent it?

I have a problem when I submit the form (pressing ENTER key or clicking mouse) - if I press it x times quickly, faster then actual view is recreated (eg ten times in one second), the form data are sent ten times and ten times are saved to database. 提交表单时遇到问题(按ENTER键或点击鼠标) - 如果我快速按x次,快一点,然后重新创建实际视图(例如一秒钟十次), 表格数据发送十次十次时间保存到数据库。

And no matter if there is some validation, eg check that data can be saved only once. 并且无论是否有一些验证,例如检查数据只能保存一次。

Next submit, after view refresh, works normally. 在视图刷新后,下一次提交正常工作。 Data are not saved, because the validator is working now.. 数据未保存,因为验证器现在正在运行..

So, malevolent user fill the form and instead of submit it normally, he is quickly clicking on submit button until the view is rebuilded and I have X records in database instead of one :-) 所以,恶意用户填写表格而不是正常提交​​,他快速点击提交按钮,直到视图被重建,我在数据库中有X记录而不是一个:-)

How can I prevent this strange behaviour? 我怎样才能防止这种奇怪的行为?

How to forbid to press submit button until view is rebuild? 如何禁止按下提交按钮,直到视图重建?

SOLUTION: 解:

//Initialize our submit flag to false
var formSubmitted = false;

/**
 * Prevent from submitting a form more than once
 * @returns {Boolean}
 */
function submitForm()
{   
  //has the form been submitted before?
  if( formSubmitted == true )
  {
     alert("This form has already been submitted!");
     return false;
  }
  formSubmitted = true;

  return true; // Submit form
}

and the submit button: 和提交按钮:

<h:commandButton ... action="#{bean.action}" onclick="return submitForm();"/>

You may want to add something like a DoubleClickFilter to your web.xml . 您可能希望在web.xml添加类似DoubleClickFilter的内容。 Another way is to work with Tokens which you validate each request and become invalid after first request so that subsequent requests fail your validation. 另一种方法是使用Tokens验证每个请求并在第一次请求后变为无效,以便后续请求无法通过验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM