You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Sébastien Boutté <se...@gmail.com> on 2008/01/24 10:48:27 UTC

Dynamic Component

Hi,

I'm trying to change dynamically component but i have got problems with
HtmlPanelGrid :
Here is the algorithm :
I have a dynamic Panel Grid which contains others HtmlPanelGrid that i want
to change content on the fly.
When i call the file index.jsp, the jsp is well displayed everything seems
to work but when i submit my page, i've got
this exception :

javax.servlet.ServletException: Index: 1, Size: 1
	javax.faces.webapp.FacesServlet.service(FacesServlet.java:154)
	org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:147)

java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
	java.util.ArrayList.RangeCheck(ArrayList.java:546)
	java.util.ArrayList.get(ArrayList.java:321)
	javax.faces.component.UIComponentBase.processRestoreState(UIComponentBase.java:719)
	javax.faces.component.UIComponentBase.processRestoreState(UIComponentBase.java:722)
	javax.faces.component.UIComponentBase.processRestoreState(UIComponentBase.java:722)
	javax.faces.component.UIComponentBase.processRestoreState(UIComponentBase.java:722)

I've searched on mailing list for solutions, i've found none. I try also to
define the methods saveState and processRestoreState but it doesn't work.

Thank you for your help,

Sebastien


Here is my code :

public class CustomComponent extends HtmlPanelGrid {

    private HtmlOutputText lblField1;

    public CustomComponent() {
        Application app = FacesContext.getCurrentInstance
().getApplication();

        lblField1 = (HtmlOutputText) app.createComponent(
HtmlOutputText.COMPONENT_TYPE);

        getChildren().add(lblField1);
    }

    public void setText(String text) {
        lblField1.setValue(text);
    }
}

public class BeanController {
    private HtmlPanelGrid grid;

    public BeanController() {
        Application app = FacesContext.getCurrentInstance
().getApplication();
        grid = (HtmlPanelGrid) app.createComponent(
HtmlPanelGrid.COMPONENT_TYPE);
        grid.setColumns(1);
        createPanelGrid();
    }

    public void createPanelGrid() {
        Application app = FacesContext.getCurrentInstance
().getApplication();
        CustomComponent customComponent = new CustomComponent();
        customComponent.setText("Field1");
        grid.getChildren().add(customComponent);

        HtmlCommandButton button = (HtmlCommandButton) app.createComponent(
HtmlCommandButton.COMPONENT_TYPE);
        button.setValue("submit");
        MethodBinding vb3 = FacesContext.getCurrentInstance
().getApplication().createMethodBinding("#{beanController.submit}",null);
        button.setAction(vb3);

        grid.getChildren().add(button);
    }


    public void createPanelGrid2() {
        Application app = FacesContext.getCurrentInstance
().getApplication();

        CustomComponent customComponent = new CustomComponent();
        customComponent.setText("Field2");
        grid.getChildren().add(customComponent);

        HtmlCommandButton button = (HtmlCommandButton) app.createComponent(
HtmlCommandButton.COMPONENT_TYPE);
        button.setValue("cancel");
        MethodBinding vb3 = FacesContext.getCurrentInstance
().getApplication().createMethodBinding("#{beanController.cancel}",null);
        button.setAction(vb3);

        grid.getChildren().add(button);
    }

    public HtmlPanelGrid getPanelGrid() {
        return grid;
    }

    public String submit() {
        System.out.println("submit");
        grid.getChildren().clear();
        createPanelGrid2();
        return null;
    }

    public String cancel() {
        System.out.println("cancel");
        grid.getChildren().clear();
        createPanelGrid();
        return null;
    }
}

<f:view>
    <h:form>
        <h:panelGrid binding="#{beanController.panelGrid}"/>
    </h:form>
</f:view>

Re: Dynamic Component

Posted by Mike Kienenberger <mk...@gmail.com>.
On 1/24/08, Sébastien Boutté <se...@gmail.com> wrote:
> I try also to
> define the methods saveState and processRestoreState but it doesn't work.

Note also that you need to use saveState and restoreState, not
processRestoreState