JSP Controls Tag Library
 

JSP Controls Tag Library: The Component Lifecycle

Activating web component

Component is activated by sending a request that contains an event parameter. An event parameter can be sent either via Submit tag when HTML form is submitted, or via Link tag that generates an HTML <A> link.

Initial Page Load

The common way to activate a component is to submit an HTTP form. The submit element must contain event name in its "event" attritubute. Recall the view fragment rendered on initial page load step:

<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>

The <jc:form> tag generates a proper HTML FORM tag, setting the name of component JSP file in its "action" attribute. Therefore, when this form is submitted, the request containing username, password and login event parameter will be sent directly to the component.

Accepting Input And Handling An Event

Because the Handler tags are placed in component's JSP file before Render tags, they get a chance to process the request first. The body of Handler tag is evaluated if value of its "event" attribute equals to event name sent with request:

<%@ 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">
    <%
      // Log out current user first
      session.removeAttribute("USER");

      // Read username and password submitted from login form
      String username = request.getParameter("username");
      String password = request.getParameter("password");

      // Store user name in session if needed to redisplay.
      // No need to redisplay password.
      session.setAttribute("username", username);

      // Account found
      if (authenticate(username, password)) {
          // Log in user
          session.setAttribute("USER", username);
          // No need to cache username after login
          session.removeAttribute("username");

      // User accout not found, generate error message
      } else {
          // Generate error message
          ArrayList messages = new ArrayList();
          String message = msgs.getMessage("login.accountnotfound");
          messages.add(message);

          // Store errors in the session for use at render phase
          session.setAttribute("MESSAGES", messages);
      }
    %>
  </jc:handler>
  <jc:handler name="logout"> ... </jc:handler>
  <jc:reload/>

  <%-- Render phase --%>

  <jc:prerender> ... </jc:prerender>
  <jc:render view="notloggedin"> ... </jc:render>
  <jc:render view="loggedin"> ... </jc:render>
</span>

If the user provided proper credentials, the user name is saved in session-scoped variable "USER". Otherwise the "USER" variable is removed from the session. This fact is used in the body of Prerender tag to select a view to display.

After the user information has been processed, the component must proceed to render phase and redisplay itself. If incorrect credentials were submitted, the Login Component should generate an error message and redisplay the login form. If credentials were correct and account was found, the Login Component should display user information and logout button.

There ar three ways to proceed to render phase from accept phase: