ActionListener 被多次调用(错误?) - Mojarra 2.1.3
我有以下按钮:
<h:commandButton
disabled="#{mybean.searching}"
binding="#{mybean.searchButton}"
actionListener="#{mybean.searchForLicenses}"
value="Search" />
当我调试时,我看到 actionListener 首先被调用两次,然后三次,接下来单击四次,依此类推。
似乎每次重新加载时,actionListener 都会被注册一次。
我正在使用 Mojarra 2.1.3(也尝试过 2.0.6)和 Tomcat 7 以及 IceFaces。
绑定是这样完成的:
private javax.faces.component.UICommand searchButton;
public void setSearchButton(UICommand searchButton) {
this.searchButton = searchButton;
}
public UICommand getSearchButton() {
return searchButton;
}
I have the following button:
<h:commandButton
disabled="#{mybean.searching}"
binding="#{mybean.searchButton}"
actionListener="#{mybean.searchForLicenses}"
value="Search" />
When I debug I see that the actionListener is called twice first, then three times, next click four times and so on.
It seems like on every reload the actionListener is registered one more time.
I'm using Mojarra 2.1.3 (also tried 2.0.6) and Tomcat 7 with IceFaces.
The binding is done that way:
private javax.faces.component.UICommand searchButton;
public void setSearchButton(UICommand searchButton) {
this.searchButton = searchButton;
}
public UICommand getSearchButton() {
return searchButton;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您将组件绑定到会话或应用程序范围的 bean 而不是请求范围的 bean,则可能会发生这种情况。这简直就是一个糟糕的设计。相同的组件将在多个请求/视图之间重用。您需要将 bean 放入请求范围内,或者完全摆脱组件绑定。
请注意,将组件直接绑定到 bean 通常是代码中某处设计不佳的标志。您认为这是解决方案的功能需求和/或问题是什么?如果您详细说明这一点,我们也许能够提出正确的方法。
另请注意,单独使用动作侦听器也是一种设计味道。我希望“searchForLicenses”是一种正常的操作方法。另请参阅操作和actionListener之间的差异。
That can happen if you've bound the component to a session or application scoped bean instead of a request scoped bean. This is simply a bad design. The very same component would be reused among multiple requests/views. You need to put the bean in the request scope, or to get rid of the component binding altogether.
Note that binding the component directly to a bean is often a sign of poor design somewhere in the code. What is it, the functional requirement and/or problem for which you thought that this is the solution? If you elaborate on that, we may be able to propose the right approach.
Also note that using an action listener alone is also a design smell. I'd expect "searchForLicenses" to be a normal action method. See also Differences between action and actionListener.
当组件使用
binding
和validator
或valueChangListener
并且支持 bean 是View
时,会发生类似的问题,< code>Session 或Application
范围。然后,相应的侦听器会在请求期间被多次调用,但不是一次(每个新请求+1 次)。一种可能的解决方案是重写 jsf 类
AttachedObjectListHolder
,它用于存储组件侦听器
。当前的实现只是将新的listener
添加到组件,即使相同的listener
已经存在。因此,建议的修复方法是在添加侦听器之前检查侦听器是否不存在。您可以在此处查看修复的详细信息
The similar issue takes place when component is using
binding
andvalidator
orvalueChangListener
and backing bean is ofView
,Session
orApplication
scope. Then corresponding listeners are called many times but not once during request (+1 time with every new request).One possible solution is to override jsf class
AttachedObjectListHolder
which is used for storing componentlisteners
. Current implementation simply add newlistener
to component even though the samelistener
is already there. So the proposed fix is to check thatlistener
does not exist before adding it.Details of the fix you can see here