0
votes

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
1
When are you calling createComponents()? Unrelated to the concrete problem, I suggest you to reconsider this approach. A <h:dataTable> or probably <p:dataGrid> or <p:dataList> is much easier. stackoverflow.com/questions/3409053/… - BalusC
I'm calling this method when bean constructor is calling. Using datatable is not good for me because I want to dynamically add the different components to the page to the places where I want. - Barbarian
Okay back to the problem, is there a <h:form> parent? - BalusC
Yes, sure, and p:panel is inside - Barbarian
Any exceptions in server log? I'd expect to see IllegalStateException here. EL cannot access/create instances of anonymous classes. See also stackoverflow.com/questions/5432897/… - BalusC

1 Answers

1
votes

Using 2.1.3 version of Mojarra instead 2.0.3 has solved problem.