JSP Controls Tag Library: The Component Lifecycle
Initial Page Load
Initial page load starts when a user navigates to a composite page from a browser (follow a green line from the top):
After the request hits the server, the server starts loading the composite page. When servlet engine reaches the <jsp:include> action, it starts loading the component. From the component's point of view, the load process appears as a separate HTTP GET request. The request generated by <jsp:include> action retains all query parameters from the original request.
Component Rendering
When request hits the component, the component verifies that request is clean, that is, it does not contain an event parameter (more on events in "Accepting user input" section).
Handler tags MUST placed in the beginning of JSP file, because they MUST be processed first. The body of a Handler tag is evaluated if request contains event corresponding to "event" attribute of Handler tag. In case of initial page loading, the request is always clean, therefore the body of Handler tags is not evaluated.
The code in the body of Prerender tag usually selects the view to display, then one of the Render tags displays a selected view. The name of a view must be set as a page- or request-scoped variable "jspcontrolsViewSelected". This variable is checked by Render tags to figure out which view to render. In the code snippet below, "notloggedin" view name is selected if the user has not logged in yet.
You may want to use JSTL conditional tags or JSP scriptlets to render a particular view instead of using Render tags. If you do so, you still have to specify an empty Prerender tag. Prerender tag performs URL housekeeping: it stores reload address in the session, it provides access to component address, it fixups the response header for Ajax mode. Don't forget to specify Prerender tag.
Render tag evaluates its body if the "view" attribute is not specified at all, or if it is equal to a view name set in "jspcontrolsViewSelected" page variable. Thus, if a user has not logged in yet, the login form is displayed:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="/WEB-INF/lib/jspcontrols.tld" prefix="jc" %> <span id="LoginComponent"> <%-- Accept phase --%> <jc:handler event="login"> ... </jc:handler> <jc:handler event="logout"> ... </jc:handler> <jc:reload/> <%-- Render phase --%> <jc:prerender> <% pageContext.setAttribute("jspcontrolsViewSelected", session.getAttribute("USER") != null ? "loggedin" : "notloggedin"); %> </jc:prerender> <jc:render view="notloggedin"> <h3>Log In</h3> <jc:form> Name: <input type="text" name="username" value="<c:out value="${username}"/>"/><br/> Password: <input type="password" name="password" value="" /> <jc:submit event="login" value="Log In"/> </jc:form> </jc:render> <jc:render view="loggedin"> ... </jc:render>
At this point the render phase has finished and the view fragment has been included into the composite page. After servlet engine returns back to the composite page, it completes the response and returns the full page to the browser. Now the component is ready to accept user input.