1

I am creating a java program which dynamically add jsf component in my xhtml page in jsf 2.0. RIght now I am facing a weird problem. Below is my .java code and .xhtml code.

Firstly my getter of HtmlPanelGrid is called before command button action event. And when it returns grid, it added extra panel grid into grid object.

So when I click second time on the button, globalGrid child count is being 2 instead of 1.

My scope in @ViewScoped.

Please help regarding this.

Thanks in advance.

testCustom.xhtml

<h:panelGrid binding="#{datatableBean.globalGrid}" />           <h:commandButton value="{bundle.newCaseInfoSheet_addAdditionalParty}" action="# {datatableBean.addNewAdditionalParties}" />

DatatableBean.java

public class DatatableBean{   

    public static HtmlPanelGrid globalGrid = new HtmlPanelGrid();
    public int gridCounter = 1;

    public void setGlobalGrid(HtmlPanelGrid globalGrid) {
        this.globalGrid = globalGrid;
    }

    public HtmlPanelGrid getGlobalGrid() {
        return globalGrid;
    }

    public HtmlPanelGrid getAdditionalParties(int i) {

            HtmlPanelGrid mainGrid = null;
            HtmlOutputText LNameText = new HtmlOutputText();

            LNameText.setId("LNameText_"+i);
            LNameText.setValue("Last Name");

            HtmlInputText LNameInputText = new HtmlInputText();
            LNameInputText.setId("LNameInputText_"+i);

            mainGrid = new HtmlPanelGrid();
            mainGrid.setId("mainGrid_"+i);
            mainGrid.setColumns(2);
            mainGrid.getChildren().add(LNameText);
            LNameInputText.setSize(45);
            mainGrid.getChildren().add(LNameInputText);

        return mainGrid;
    }

    public void addNewAdditionalParties() {

        System.out.println("\n In globalGrid Start -------->" + globalGrid.getChildCount() + "" );

        if(gridCounter >= 2)
        {
            globalGrid = (HtmlPanelGrid)session.getAttribute("globalGrid");
            globalGrid.getChildren().clear();
            System.out.println("Grid COunt is --> " + gridCounter);
            for(int i =1; i<=gridCounter; i++)
            {
                globalGrid.getChildren().add(getAdditionalParties(i));
            }
        }
        else
        {
            globalGrid.getChildren().add(getAdditionalParties(1));
        }
        gridCounter++;
        System.out.println("Grid Count after is --> " + gridCounter);
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Kevin Shah
  • 41
  • 3
  • Your issue may be liked: http://stackoverflow.com/questions/10428411/primefaces-jsf-can-not-update-binding-component – Rong Nguyen Apr 09 '13 at 06:10
  • thank a lot @RongNK, but my issue somewhat different from what you have suggested. The main issue is why getter is called before command button event. – Kevin Shah Apr 09 '13 at 07:54
  • oh, so you can refence: http://stackoverflow.com/questions/2692324/jsf-getter-methods-called-before-beforephase-fires – Rong Nguyen Apr 09 '13 at 08:28
  • I think getter is called before command button event is true because of JSF life cycle:http://balusc.blogspot.com/2006/09/debug-jsf-lifecycle.html – Rong Nguyen Apr 09 '13 at 08:43
  • thank you @RongNK, I fully understood the lifecycle of JSF. But because of this, when I first clicked on button, it add nothing and on second click it adds directly two component. IT means it skips the first component. I just changed my .xhtml code as below. Now I get grid as per adding but on UI it displays after second click. – Kevin Shah Apr 09 '13 at 09:29
  • And changed my globalGrid from static to normal. After that I am facing this problem. – Kevin Shah Apr 09 '13 at 09:37
  • @Divyesh: please stop adding [java] tag to Java EE API related questions (JSF, JSP, Servlets, etc) which are not demonstrable using a plain Java application class with main() method nor answerable with JLS. This will limit noise for [java] tag followers and this will also avoid knee-jerk responses from [java] nitwits who know nothing about Java EE which would only potentially confuse the OP. This question is about JSF, so add [jsf] tag only. – BalusC Sep 02 '16 at 06:36
  • @BalusC sorry for that it's JSF related question. – Divyesh Kanzariya Sep 02 '16 at 06:58

0 Answers0