Wiki

Clone wiki

Oracle JSF Expert 1Z0-896 / CDI Named bean to handle action and valuechange events

Action and ValueChange events

javax.faces.event.ValueChangeListener interface

public interface ValueChangeListener extends FacesListener {
   public void processValueChange(ValueChangeEvent event);
}

javax.faces.event.ActionListener interface

public interface ActionListener extends FacesListener {
   public void processAction(ActionEvent event);
}

Implementing f:actionListener with a CDI Named ActionListener

@Named
public class MyActionListener implements ActionListener {

    @Override
    public void processAction(ActionEvent event) {
        // Do something
    }

}
<h:commandButton value="Submit" >
    <f:actionListener binding="#{myActionListener}" />
</h:commandButton>

Implementing actionListener attribute and f:actionListener with a CDI Named Bean and a ActionListener method

@Named
public class MyBean {

    public void processAction(ActionEvent event) {
        // Do something
    }

    public void processAnotherAction() {
        // Do something else
    }

    public void processYetAnotherAction() {
        // Do something else
    }

}

Listeners are executed in order of reference starting with actionListener attribute

<h:commandButton value="Submit" actionListener="#{myBean.processAction}" >
    <f:actionListener binding="#{myBean.processAnotherAction()}" />
    <f:actionListener binding="#{myBean.processYetAnotherAction()}" />
</h:commandButton>

Implementing f:valueChangeListener with a CDI Named ValueChangeListener

@Named
public class MyValueChangeListener implements ValueChangeListener {

    @Override
    public void processValueChange(ValueChangeEvent event) {
        // Do Something
    }

}
<h:inputText value="#{myBean.value}">
    <f:valueChangeListener binding="#{myValueChangeListener}" />
</h:inputText>

Implementing valueChangeListener attribute and f:valueChangeListener with a CDI Named Bean and a ValueChangeListener method

@Named
public class MyBean {

    private String value;

    public void processValueChange(ValueChangeEvent event) {
        // Do something
    }

    public void processAnotherValueChange() {
        // Do something else
    }

    public void processYetAnotherValueChange(ValueChangeEvent event) {
        // Do something else
    }

}

Listeners are executed in order of reference starting with valueChangeListener attribute

<h:inputText value="#{myBean.value}" valueChangeListener="#{myBean.processValueChange}" >
    <f:valueChangeListener binding="#{myBean.processAnotherValueChange()}" />
    <f:valueChangeListener binding="#{myBean.processYetAnotherValueChange(event)}" />
</h:inputText>

Updated