Set actionListener of primefaces CommandButton object in managed bean using binding

1.4k views Asked by At

I am using primefaces binding , I am trying to add actionListener to at the managed bean but I don't know how to do that.

Also when I used

commandButton.setActionListener(MethodBinding b);

this method is deprecated , can anyone help me with this?

1

There are 1 answers

0
wittakarn On BEST ANSWER

The method setActionListener is deprecated. This has been replaced by addActionListener(javax.faces.event.ActionListener).

Thus, you should use addActionListener instead.

xhtml

<h:body>
    <h:form>
        <p:commandButton value="execute" binding="#{buttonView.button}"/>
    </h:form> 
</h:body>

managedbean

/**
 *
 * @author Wittakarn
 */
@SessionScoped
@ManagedBean(name = "buttonView")
public class ButtonView implements Serializable{

    private CommandButton button;

    public CommandButton getButton() {
        return button;
    }

    public void setButton(CommandButton button) {
        this.button = button;
    }

    @PostConstruct
    public void init(){
        button = new CommandButton();
        button.addActionListener(new CommandButtonActionListener());
    }
}

CommandButtonActionListener.java

/**
 *
 * @author Wittakarn
 */
public class CommandButtonActionListener implements ActionListener{

    @Override
    public void processAction(ActionEvent event) throws AbortProcessingException {
        System.out.println("execute");
    }
}