会话范围的托管 bean 和 actionListener
我想使用同一个按钮对不同的托管 bean 执行多个操作,一个是作用域会话,另一个是请求。在我的示例中,我对两者使用相同的 bean。
index.xhtmlcontroller.Controller.java
<h:form>
<p:commandButton image="ui-icon ui-icon-notice" action="#{controller.inc()}" update="result">
<f:actionListener type="controller.Controller" />
</p:commandButton>
</h:form>
<p:panel id="result">
#{controller.count}
</p:panel>
当我按下按钮时,
@Named(value = "controller")
@SessionScoped
public class Controller implements ActionListener, Serializable
{
int count = 0;
public Controller(){
System.out.println("new");
}
public void inc(){
count += 1;
}
public int getCount(){
return count;
}
@Override
public void processAction(ActionEvent event) throws AbortProcessingException{
count += 1000;
}
}
计数增加 1,而不是 1001,并创建一个新的 bean。我做错了什么?
谢谢。
I want to do multiple actions on different managed beans with the same button, one being scoped session and the other request. In my example I use the same bean for both.
index.xhtml
<h:form>
<p:commandButton image="ui-icon ui-icon-notice" action="#{controller.inc()}" update="result">
<f:actionListener type="controller.Controller" />
</p:commandButton>
</h:form>
<p:panel id="result">
#{controller.count}
</p:panel>
controller.Controller.java
@Named(value = "controller")
@SessionScoped
public class Controller implements ActionListener, Serializable
{
int count = 0;
public Controller(){
System.out.println("new");
}
public void inc(){
count += 1;
}
public int getCount(){
return count;
}
@Override
public void processAction(ActionEvent event) throws AbortProcessingException{
count += 1000;
}
}
When I press the button the count increases by 1, instead of 1001, and creates a new bean. What did I do wrong ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是预期的行为。
在每个声明上创建并获取自己的 bean 实例。它不重用由 JSF 管理的相同会话作用域 bean。您需要使用
binding
来绑定到已创建的会话作用域 bean 实例。That's expected behaviour. The
<f:actionListener type>
creates and gets its own bean instance on every declaration. It does not reuse the same session scoped bean which is managed by JSF.You need to use
binding
instead to bind to the already-created session scoped bean instance.