Barbarian
Barbarian

Reputation: 11

Why does commandButton action listener does not working?

I'm using Primefaces components with JSF2.0 and Tomcat 6. The goal is to dynamically create a server components from bean. Just for test I did the next thing:

    private Panel editorPanel;    
    private CommandButton btn;

    public void createComponents()
    {
        this.setEditorPanel(new Panel());
        this.getEditorPanel().setRendered(true);

        btn = new CommandButton();
        btn.setId("btn1");
        btn.setRendered(true);
        btn.setAjax(true);
        btn.setValue("Click!");        

        btn.addActionListener(new ActionListener()
        {
            @Override
            public void processAction(ActionEvent arg0) throws AbortProcessingException
            {
                addNewButton();                
            }
        });

        this.getEditorPanel().getChildren().add(btn);
    }


    public void addNewButton()
    {
        CommandButton btn2 = new CommandButton();
        btn2.setRendered(true);        

        this.getEditorPanel().getChildren().add(btn2);
    }

Markup:

    <p:panel binding="#{mybean.editorPanel}">

    </p:panel>

At the first page load button is rendered. After pressing the button, my bean is recreating due to the request, but breakpoint at "addNewButton" method is never triggered. After that request, the button is disappered. Bean is marked as @RequestScoped and @ManagedBean(name="mybean"). The other components from markup that are not dynamically rendered can successfully use bean methods. What I'm doing wrong with this button?

Thanks

EDIT: I know that I can add the button as the previous one in the same method, but the goal is to dynamically add some components.

EDIT 2: I made the separate implementation of ActionListener as the public inner class, and I get the next:

           Caused by: java.lang.InstantiationException:web.beans.pages.TemplateEditBean$BtnListener
           at java.lang.Class.newInstance0(Unknown Source)
           at java.lang.Class.newInstance(Unknown Source)
           at javax.faces.component.StateHolderSaver.restore(StateHolderSaver.java:103)
... 90 more

Upvotes: 0

Views: 1184

Answers (1)

Barbarian
Barbarian

Reputation: 11

Using 2.1.3 version of Mojarra instead 2.0.3 has solved problem.

Upvotes: 1

Related Questions