Executing commands when a page loads

A JSF Handler can include functions that run automatically when the page loads.

The JSF Handler properties onPreRenderFunction, onPostRenderFunction, and onConstructionFunction allow you to specify functions that run at different points in the page's life cycle.

When these functions run depends on the value of the scope property of the page. When the user visits an EGL-controlled web page for the first time, EGL loads the JSF Handler, which is represented at run time by a page bean. If the scope of the page is set to session (the default), the bean remains active during the user's session, even if the user browses away from the page. If the user returns to the page, the bean is still available. If the scope of the page is set to request, the bean does not remain active when the user browses away from the page. In this case, any data stored in the bean is lost, and the bean must be loaded again if the user returns to the page.

If you specify more than one of these properties, be aware of the following:
The following example of a JSF Handler illustrates the use of two of these functions:
  1. In an EGL web project, create a new web page named loadTest.jsp.
  2. Open the JSF Handler of the new page by right-clicking the open page in the editor and then clicking Edit Page Code.
  3. Change the code in the JSF Handler to match this example:
    package jsfhandlers;
    
    handler loadTest type JSFHandler
        {onConstructionFunction = onConstruction, 
         onPreRenderFunction = onPreRender,
         scope = session,
         view = "loadTest.jsp"} 
          
          numberOfLoads int;
          messageString string;
    
        function onConstruction()
            numberOfLoads = 0;
        end
        
        function onPreRender(incomingNumber int)
            numberOfLoads = incomingNumber + 1;
            messageString = "You have viewed this page " 
                + numberOfLoads + " times.";
        end
        
        function forwardBack()
            forward numberOfLoads to "loadTest";
        end
        
    end
  4. Save the JSF Handler and close it.
  5. On the loadTest.jsp page, add an Output field from the Enhanced Faces Components drawer of the Palette view.
  6. Next to the output field, add a Button - Command from the Enhanced Faces Components drawer of the Palette view.
  7. From the Page Data view, drag messageString directly onto the output field. Now the variable in the JSF Handler is bound to the output field on the page.
  8. From the Page Data view, drag forwardBack() directly onto the command button. Now the button is bound to the function in the JSF Handler.
  9. Save the page and generate the project.
  10. Run the page on a server.
The first time that you run this page, it displays "You have viewed this page 1 times." In this case, the function specified in the onConstructionFunction property runs first and sets the numberOfLoads variable to zero. Then, the function specified in the onPreRenderFunction property runs, sets the variable to 1, and sets the message string variable to "You have viewed this page 1 times." Each subsequent time you reload the page, the variable will increase by one, demonstrating that the onPreRenderFunction function is running each time, but the onConstructionFunction function is not.

If you try the example with scope set to request, the variable will never exceed 1 because the onConstructionFunction function sets the variable to zero each time you refresh the page. Remember to save your changes to the JSF Handler, generate, and republish the project to the server.

These functions have the following limitations:

For more information, see the topics dedicated to these properties.


Feedback