会话范围的托管bean和actionListener [英] Session scoped managed bean and actionListener

查看:166
本文介绍了会话范围的托管bean和actionListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用相同的按钮对不同的受管bean进行多项操作,一个是作用域会话,另一个是请求.在我的示例中,我对两者都使用相同的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

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

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;
    }
}

当我按下按钮时,计数将增加1,而不是1001,并创建一个新bean.我做错了什么?

When I press the button the count increases by 1, instead of 1001, and creates a new bean. What did I do wrong ?

谢谢.

推荐答案

这是预期的行为. <f:actionListener type>在每个声明中创建并获取自己的bean实例.它不会重用由JSF管理的同一会话范围的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.

您需要使用binding绑定到已经创建的会话作用域的bean实例.

You need to use binding instead to bind to the already-created session scoped bean instance.

<f:actionListener binding="#{controller}" />

这篇关于会话范围的托管bean和actionListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆