Session-driven bean and actionListener

I want to do several actions on different managed beans using the same button, one of which is a scope session and another 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 score increases by 1 instead of 1001 and creates a new bean. What have I done wrong?

Thank.

+5
source share
1 answer

Expected Behavior. <f:actionListener type>creates and gets its own bean for each ad. It does not reuse the same bean session area that is managed by JSF.

binding bean.

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

All Articles