1 2 Previous Next 26 Replies Latest reply on Mar 19, 2009 10:03 AM by Nick Belaevski

    Bean executed twice - while calling and exiting the web page

    E Aish Novice

      Hi,

      I have a .jsp file that displays the backend data table contents using a DataTable tag. When the page containing this dataTable is selected, it executes the backend bean and displays the results. This is fine.

      But when we exit from that page and go to the next page (that is select a menu to go to the next page), the bean in the first page is getting executed again (during the exit of that page). Not sure why the bean is executed during the exit of the page.

      Can you please help me understand this. This has a performance issue as the table is big and we do not want to execute the bean while getting out of the web page.

      For your reference, My bean is declared as session scope. I am also giving the .jsp page and the bean code for your reference.


      Thanks
      Aish

      This is the bean:
      ==============

      public List getDisplayinputqueuetbl()
      {
      Connection conn = null;
      ResultSet iTblResult = null;
      iTblList=new ArrayList();
      try
      {
      conn = datasource.getConnection();

      Statement iTblStmt=conn.createStatement();
      iTblResult = iTblStmt.executeQuery(inSQL);

      while(iTblResult .next())
      {

      this.iTblList.add (new ITblInfo(iTblResult .getString("fld12"),
      iTblResult .getString("name") ));

      }

      }
      catch(SQLException e)
      {
      System.out.println("SQL Exception thrown in Table " + e.getMessage());
      e.printStackTrace();
      }

      catch(Exception e1)
      {
      System.out.println("Exception thrown in Table " + e1.getMessage());
      e1.printStackTrace();
      }
      finally
      {
      closeDBConnection(conn);
      }

      return this.iList;
      }

      The .jsp page is
      ==============

      <%@
      taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
      <%@
      taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
      <%@
      taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
      <%@
      taglib uri="http://richfaces.org/rich" prefix="rich"%>
      <rich:dataTable value="#{TableDetail.displayitbl}"
      var="ie"
      id="tdbl"
      border="1"
      headerClass="tableHeader"
      rowClasses="tableRows" >
      <f:facet name="header" >
      <h:outputText value="Table" />
      </f:facet>
      <rich:column >
      <f:facet name="header" >
      <h:outputText value="fld1" />
      </f:facet>
      <h:outputText value="#{ie.fld1}" />
      </rich:column>
      </rich:dataTable>

        • 1. Re: Bean executed twice - while calling and exiting the web
          Nick Belaevski Master

          Hi,

          How do you exit to the next page, by h:commandLink or by h:outputLink?

          • 2. Re: Bean executed twice - while calling and exiting the web
            E Aish Novice

            hi,

            I am using rich:menuItems to navigate between various pages. Let me post my code below for your reference. (I have given only a small section of my code due to space constraints).

            Please let me know if you need additional information
            Thanks
            Aish

            This is my index page (main page in my web):
            =============

            <h:form id="myForm">




            This is the header section of the web page




            <a4j:include viewId="/left-menu.jsp" />





            <a4j:include viewId="/topmenu.jsp" />

            <a4j:outputPanel id="centerGrid" >
            <a4j:include viewId="#{link.address}">
            </a4j:include>






            Footer section



            </h:form>

            This is topmenu.jsp
            =============

            <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
            <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
            <%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
            <%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>

            <rich:toolBar width="100px">

            <rich:dropDownMenu value="Sample1">
            <rich:menuItem value="MenuItem1" action="#{link.domenuItem1}" id="dropdownmenu_mItem1" />
            <rich:menuItem value="MenuItem2" action="#{link.domenuItem2}" id="dropdownmenu_mItem2" />


            </rich:dropDownMenu>
            </rich:toolBar>

            ====

            This is link.java
            ===========

            public void domenuItem1()
            {
            urllink="/pageMenu1.jsp";

            }

            public void domenuItem2()
            {
            urllink="/pageMenu2.jsp";

            }

            public String getlink()
            {
            return urllink;

            }







            • 3. Re: Bean executed twice - while calling and exiting the web
              Nick Belaevski Master

              So, this happens because components nested inside data table are trying to process submitted information.

              Workaround: develop phase listener that will track JSF lifecycle phases and skip data table processing if we're on 2-5 phases. Data table processing can be skipped by setting rendered="false", e.g.:

              <rich:dataTable rendered="#{onEncodePhase}" />


              Where onEncodePhase is a request-scoped boolean parameter that will be added by phase listener on the 6th phase start.

              • 4. Re: Bean executed twice - while calling and exiting the web
                E Aish Novice

                Hi,

                Thanks for responding.

                Would you be able to post some code on how to track JSF life cycle phase using phase listeners. I am new to this.

                I am not sure If I clearly explained this in my earlier posts. When I select menuItem1, it sets the urlLink and reRenders the page. In that page, we have the back end bean that gets the data from the table. After this dataTable is displayed on the screen, I select menuItem2 using the menuoption and it sets the urlLink to the new .jsp page and reRenders the page. While coming out of menuItem1 and entering into menuItem2, the system executes the back end beans in menuItem1 program and menuItem2 program.

                Can you please help me understand this on how to control with JSF life cycles?


                Thanks
                Aish



                • 5. Re: Bean executed twice - while calling and exiting the web
                  Nick Belaevski Master

                  Now it's much clear. Let's postpone phase listener development and try something simpler. Please try setting ajaxSingle="true" for menu items.

                  • 6. Re: Bean executed twice - while calling and exiting the web
                    E Aish Novice

                    Hi,

                    Thanks for responding.

                    I tried that and still got the same results -that is the bean was executed during the entry and exit of the program. I also tried submittype="ajax" but still the same results.

                    Please let me know what else Ican try.

                    Thanks
                    Aish

                    • 7. Re: Bean executed twice - while calling and exiting the web
                      Nick Belaevski Master

                      Aish,

                      You can try the workaround described here: https://jira.jboss.org/jira/browse/RF-3341. Set AJAX submission mode and check.

                      • 8. Re: Bean executed twice - while calling and exiting the web
                        E Aish Novice

                        Hi,

                        I tried the following two options and they didn't work. That is the bean is called during entry and exit of the program. Please let me know if what I tried is wrong

                        Thanks
                        Aish

                        Option 1
                        ========
                        <a4j:keepAlive beanName="tabDetail" />

                        <rich:dataTable id="tabDetal_dTable"
                        value="#{tabDetail.displayTbl}" var="dbqueue" border="1"
                        width="670px" cellspacing="1" cellpadding="1" >


                        Option 2
                        ========
                        <a4j:region renderRegionOnly="true">
                        <rich:dataTable id="tabDetal_dTable"
                        value="#{tabDetail.displayTbl}" var="dbqueue" border="1"
                        width="670px" cellspacing="1" cellpadding="1" >


                        </a4j:region>



                        • 9. Re: Bean executed twice - while calling and exiting the web
                          Nick Belaevski Master

                          Hi Aish,

                          You should wrap "AJAXed" menu items into a4j:region. Unfortunately it's not clear whether menu items are inside the data table or not.

                          • 10. Re: Bean executed twice - while calling and exiting the web
                            E Aish Novice

                            Hi,

                            Please help me with this. I have tried a4j:region and it didn't help me. May be I missed something.

                            The problem I have is - I select a menu to display a web page which connects to backend database and displays the data using a rich:dataTable. Then I select a different menu to go a new web page. Here, the first program is called again which connects to the database and extracts the data and then, go to display the second web page.Basically,
                            the bean method of the first program is executed during the entry and exit of web page 1. Again, when I move from second web page to third web page, the bean method of second program is executed both during entry and exit of the program. This way bean method execution happens during the entry and exit of all program which impacts performance.

                            Could you please help me to resolve this situation. I am explaining below the way menu item is handled for your reference.

                            Please refer to the index.jsp page. I have the dropdown menu.jsp at the top of the page and just below the header. The dropdown.jsp is given for your reference. When a menu Item is selected, it sets the "link" variable in the navigate.java to the corresponding .jsp and that .jsp file is displayed in the outputPanel ('centergrid") defined on the index.jsp.

                            The beans are defined in 'session' scope.

                            Please let me know if you need additional information to help me.

                            Thanks
                            Aish


                            index.jsp
                            =======

                            <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
                            <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
                            <%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
                            <%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>


                            <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
                            default-menu

                            <f:view>

                            <h:form id="myForm">




                            This is is the header section with just html tags







                            <a4j:include viewId="/dropdownmenu.jsp" />

                            <h:panelGrid columns="3" style="width:775px">
                            <a4j:outputPanel id="centerGrid" >
                            <a4j:include viewId="#{navigate.address}">
                            </a4j:include>

                            </a4j:outputPanel>
                            </h:panelGrid>




                            This is the footer section



                            </h:form>


                            </f:view>


                            dropdownmenu.jsp
                            ===============

                            <rich:toolBar width="775px">

                            <rich:dropDownMenu value="Section A">
                            <rich:menuItem value="System A" action="#{navigate.doSystemA}" id="dropdownmenu_mItem1" />
                            <rich:menuItem value="System B" action="#{navigate.doSystemB}" id="dropdownmenu_mItem2" />
                            </rich:dropDownMenu>


                            Navigate.java
                            ===========
                            class navigate
                            {

                            private String link;

                            public void doSystemA()
                            {
                            link = "SystemA.jsp"
                            }

                            public void doSystemB()
                            {
                            link = "SystemB.jsp"
                            }
                            public String getAddress()
                            {
                            return urllink;

                            }

                            }

                            • 11. Re: Bean executed twice - while calling and exiting the web
                              Nick Belaevski Master

                              Can you please post full page code containing a4j:region components?

                              • 12. Re: Bean executed twice - while calling and exiting the web
                                Ilya Shaikovsky Master

                                Normal JSF behavior. You using dropDownMenu in mode server. So not ajax but standard post occurs. your menu and table in the same submitted form. So table called datamodel during decodes. And regions will not helps you as them works only during ajax requests.

                                You should define menu item as immediate. But be carefull if you need to submit some changes from the dataTable (for example if you will need the table to be editable)

                                • 13. Re: Bean executed twice - while calling and exiting the web
                                  E Aish Novice

                                  Hi,

                                  Thanks for reviewing my post and passing on your suggestions.

                                  1. As you have suggested, I tried using immediate="true" in my menuitem (my tables are not editable) and it didn't help. As usual, The bean was called twice - during the entry and exit of the program.

                                  2. Then, I set the submitmode="ajax" and immediate="true". I noticed the same behaviour. The bean was called twice.

                                  Please let me know what else I could change to make this work?

                                  Also, I do have a menu implemented at the left side of the index page. This was implemented using panelMenuItem and panelmenugroup. (I have dropdown menu at the top of the page and this panel menu at the left hand side of the page.) I am enclosing the code of this panelmenu below for your reference. This panelmenu also behaves in the same way - bean getting called twice.

                                  Please advise.
                                  Thanks
                                  Aish

                                  panelMenu.jsp
                                  ==========
                                  <rich:panelMenu id="mainMenu"
                                  mode="ajax" iconExpandedGroup="disc" iconCollapsedGroup="disc"
                                  iconExpandedTopGroup="chevronUp" iconGroupTopPosition="right"
                                  iconCollapsedTopGroup="chevronDown" style="width:170px;background-color:#B0E0E6;border:thin solid blue"
                                  expandSingle="true" styleClass="menustyle" >

                                  <rich:panelMenuGroup label="Section A"
                                  expanded="true" immediate="true" ignoreDupResponses="true" name="tables">

                                  <rich:panelMenuItem label="System A" name="System A"
                                  action="#{navigate.doSystemA}" reRender="centerGrid" styleClass="menustyle">
                                  </rich:panelMenuItem>

                                  <rich:panelMenuItem label="System B" name="System B"
                                  action="#{navigate.doSystemB}"reRender="centerGrid" styleClass="menustyle">
                                  </rich:panelMenuItem>

                                  </rich:panelMenuGroup>

                                  </rich:panelMenu>

                                  • 14. Re: Bean executed twice - while calling and exiting the web
                                    Nick Belaevski Master

                                    Enclose components that generate requests (e.g. that have "action" attribute in reference guide/tld) into a4j:region components. Data tables should not be included into regions. Check livedemo for "region" component to see how this works.

                                    1 2 Previous Next