如何在 JSF 中同步来自同一用户的两个请求?
基本上,我的问题是,如果用户单击一个按钮,并等待页面响应,然后单击另一个按钮,似乎存在竞争条件。
如何避免第一个按钮的代码使用与第二个按钮的代码相同的变量?
我怀疑这两个请求都会以某种随机顺序调用相同的 getter 和 setter,因为每次按钮单击都将处于 JSF 模型的不同阶段。
这是一个例子。假设用户单击按钮,操作函数会更改某些变量。该变量通过 getter 和 setter 链接到实际的 jsf 标记。现在假设操作函数已完成 50%。现在用户点击相同的按钮。 JSf 将调用链接到同一变量的 getter 和 setter,恢复状态或其他。基本上,该变量会重置回操作更改它之前的状态。现在,第一次单击的操作函数继续运行,并且它再次更改变量。即使没有第二个操作调用,这也是一个竞争条件。
Basically, my problem is that if the user click the one button, and does wait for the page to respond, and then clicks another button, it seems like there is a race condition.
How can I avoid the code for the first button using the same variables as the code for the second button?
I suspect that both requests are going to call the the same getters and setters in some random order, because each button click will be in a different phase of the JSF model.
Here is an example. Suppose the user clicks a button, the action function changes some variable. That variable is linked with a getter and a setter to an actuall jsf tag. Now suppose the action function is 50% complete. Now the user hits the same button. JSf will call the getters and the setters linked to that same variable, restoring the state or whatever. Basically, that variable is reset back to what it was before the action changed it. Now the action function for the first click is continuing to run, and it changes the variable again. Even with out the second action call, it is a race condition.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在客户端,您可能需要查看 JavaScript 来禁用多次提交,例如在单击后禁用提交输入类型。但这无论如何都不是一个万无一失的机制。
在 JSF 本身中,它取决于 Bean 的范围。如果它是会话范围的,您可以同步对其操作方法的访问(即调用应用程序方法),因此一次只有一个线程可以执行该逻辑。同样,您可以同步对共享数据的访问。不过,这仅在您具有会话关联性时才有效。如果你不这样做,那么你就会挣扎!
但是,组件树本身(更新值、流程验证等)仍将被第二个请求调用。也许您可以查看 PhaseListener 或其他东西来防止这种情况,但这似乎很棘手,而且可能有点矫枉过正。
一般来说,应用程序的编码和构建方式意味着您应该能够处理同一用户的多个请求。尽管我个人倾向于使用 JavaScript 方法来防止多次表单提交。您想更详细地解释一下您发现的问题吗?也许用代码片段?
Well on the client side you may want to look at JavaScript to disable multiple submissions e.g. disable submit input type after it's clicked. This isn't a foolproof mechanism by any means though.
In JSF itself it depends upon the scope of the Bean. If it's session scoped you could synchronize access to its action method (i.e. invoke application method) so only one thread at a time can perform the logic. Likewise you could synchronize access to the shared data. This will only work if you have session affinity though. If you don't then you're going to struggle!
However, the component tree itself (update values, process validations, etc.) will all still be invoked for the second request. Maybe you could look at a PhaseListener or something to prevent this, but this seems tricky and maybe an overkill.
Generally the way your application is coded and built means that you should be able to handle multiple requests by the same user. Although personally I go for the JavaScript approach to prevent multiple form submissions. Do you want to explain in some more detail, maybe with code snippets, what problem you perceive?