12.1. Using JSTL: An Overview

The standard tag library consists of two sublibraries. Each of the two sublibraries contains tags for general looping, iterating over the elements in a variety of data structures, evaluating items conditionally, and setting attributes or scripting variables.

The jr and jx Libraries

The first thing to understand about JSTL is that it consists of two nearly identical libraries. The first, the “jr” library, permits request time expressions as the values of its attributes. That is, it specifies rtexprvalue="true" in the attribute definitions of the TLD file. The second library, the “jx” library, uses a shorthand expression language to access page attributes and bean properties. In this library, “ $ ” indicates access to an attribute and “ . ” indicates access to a bean property. So, for example, $customer.name refers to the result of the getName method applied to the object stored in the attribute named customer.

In general, the jr library is better when your JSP page directly computes values. The jx library is usually better when you use the MVC architecture (Section 3.8) wherein a servlet computes all the important values, stores them in an attribute, and forwards the request to a JSP page that merely extracts and displays the values. However, many situations fall into a gray area between these two extremes. In such cases, the choice of libraries is mostly a matter of taste.

Core Approach

The jr library is usually better when your JSP pages directly compute values. The jx library is usually better when your JSP pages simply access existing attributes (as is the case when you use the MVC architecture).


For example, JSTL defines an if tag that conditionally outputs some result. Suppose that a servlet has stored a value of type Employee in a PageContext attribute named employee and has then forwarded the request to a JSP page. In such a case, the jr library would use the if tag as follows to conditionally output the email address.

<% Employee employee = 
     (Employee)pageContext.getAttribute("employee"); %> 
<jr:if test='<%= employee.getName().equals("andreesen") %>'> 
  Andreesen's email: <%= employee.getEmailAddress() %> 
</jr:if> 

Don’t worry about the details of this code; the if tag is discussed in Section 12.6 (Evaluating Items Conditionally). Just note the use of a JSP scriptlet to access the attribute and the use of JSP expressions to access methods of the object stored in the attribute. The jx library would use the following code instead:

<jx:if test='$employee.name == "andreesen"'> 
  Andreesen's email: <jx:expr value="$employee.emailAddress"/> 
</jx:if> 

Again, don’t worry about the details of this code. Just note that $employee is used to access the attribute named employee, .name is used to access the name bean property (i.e., the result of the getName method), and .emailAddress is used to access the emailAddress bean property (i.e., the result of the getEmailAddress method).

Now, I said that the dollar sign provided access to “attributes” from within jx code. Attributes of what? Well, by default the system first looks in the PageContext object (i.e., the pageContext predefined variable), then the HttpServletRequest object (i.e., the request predefined variable), then the HttpSession object (i.e., the session predefined variable), then the ServletContext object (i.e., the application predefined variable). But, you can also use qualifiers to change where the system looks. Here is a summary:

  • $name. Look for the attribute in the PageContext object, the HttpServletRequest object, the HttpSession object, the ServletContext object. Use the first match found.

  • $page:name. Look only in the PageContext object for the attribute named name.

  • $request:name. Look only in the HttpServletRequest object for the attribute named name.

  • $session:name. Look only in the HttpSession object for the attribute named name.

  • $app:name. Look only in the ServletContext object for the attribute named name.

  • $header:name. Call HttpServletRequest.getHeader(name).

  • $param:name. Call ServletRequest.getParameter(name).

  • $paramvalues:name. Call ServletRequest.getParameterValues(name).

The forEach Iteration Tag

The single most important tag in JSTL is forEach. It provides the ability to loop a specific number of times or to iterate down a data structure. Details are given in Section 12.3 (Looping with the forEach Tag), but here is a quick summary.

  • Looping a specific number of times (jr library). Use the var, begin, end, and, optionally, step attributes. The iteration count is stored in the PageContext attribute named by var. Access the attribute with a JSP expression. For example:

    <jr:forEach var="name" begin="x" end="y" step="z"> 
      Blah, blah <%= pageContext.getAttribute("name") %> 
    </jr:forEach> 
  • Looping a specific number of times (jx library). Use the same basic syntax as with the jr library, but access the attribute with the expr tag. For example:

    <jx:forEach var="name" begin="x" end="y" step="z"> 
      Blah, blah <jx:expr value="$name"/> 
    </jx:forEach> 
  • Looping down a data structure (jr library). Use the var and items attributes. The items attribute specifies an array, Collection, Iterator, Enumeration, Map, ResultSet, or comma-separated String. It is also legal to use begin, end, and step so that only some of the items are accessed. Use a JSP expression to define the items and to access the attribute within the loop. For example:

    <jr:forEach var="name" 
                items="<%= expression %>"> 
      Blah, blah <%= pageContext.getAttribute("name") %> 
    </jr:forEach> 
  • Looping down a data structure (jx library). Use the same basic syntax as with the jr library, but define the items by giving the name of an existing attribute and access the attribute with the expr tag. For example:

    <jx:forEach var="name" 
                items="$existing-attribute-name"> 
      Blah, blah <jx:expr value="$name"/> 
    </jx:forEach> 

The forTokens Iteration Tag

The forTokens tag lets you iterate down a String, using characters of your choice as delimiters between tokens. Details are given in Section 12.5 (Looping with the forTokens Tag), but here is a quick summary.

  • Looping down a String (jr library). Use the items attribute to specify the String and the delims attribute to specify the delimiters. The var attribute gives the name of the attribute that will store each token. Use a JSP expression to access the attribute from within the loop. For example:

    <jr:forTokens var="name" 
                  items="string" 
                  delims="characters"> 
      Blah, blah <%= pageContext.getAttribute("name") %> 
    </jr:forTokens> 
  • Looping down a String (jx library). Use the same basic syntax as with the jr library, but access the attribute with the expr tag. For example:

    <jx:forTokens var="name" 
                  items="string" 
                  delims="characters"> 
      Blah, blah <jx:expr value="$name"/> 
    </jx:forTokens> 

Conditional Evaluation Tags

The if and choose tags let you output different content depending on the results of various tests. Details are given in Section 12.6 (Evaluating Items Conditionally), but here is a quick summary.

  • The if tag (jr library). Use a JSP expression for the test attribute; if the result is true or Boolean.TRUE, the contents of the tag are evaluated. For example:

    <jr:if test="<% expression %>"> 
      Blah, blah. 
    </jr:if> 
  • The if tag (jx library). Use the same basic syntax as with the jr library, but specify the test by accessing an existing attribute and comparing it to another value using one of a small set of relational operators. For example:

    <jx:if test="$attribute.beanProperty == 'value'"> 
      Blah, blah. 
    </jx:if> 
  • The choose tag (jr library). Use nested when tags; the contents of the first one whose test attribute evaluates to true or Boolean.TRUE is used. If no when tag succeeds and there is an otherwise tag, its contents are used. Use JSP expressions to specify each of the tests. For example:

    <jr:choose> 
      <jr:when test="<% expression1 %>">Blah, blah</jr:when> 
      <jr:when test="<% expression2 %>">Blah, blah</jr:when> 
      ... 
      <jr:when test="<% expressionN %>">Blah, blah</jr:when> 
      <jr:otherwise>Blah, blah</jr:otherwise> 
    </jr:choose> 
  • The choose tag (jx library). Use the same basic syntax as with the jr library, but specify the tests as with the jx version of the if tag. For example:

    <jx:choose> 
      <jx:when test="$att.prop1 == 'val1'">Blah, blah</jx:when> 
      <jx:when test="$att.prop2 == 'val2'">Blah, blah</jx:when> 
      ... 
      <jx:when test="$att.propN == 'valN'">Blah, blah</jx:when> 
      <jx:otherwise>Blah, blah</jx:otherwise> 
    </jx:choose> 

Expression Language Support Tags

The set, expr, and declare tags let you define attributes, evaluate expressions, and declare scripting variables. The first two tags are available only in the jx library; declare is technically available in either library but is primarily used with jx. Details are given in Section 12.7 (Using the Expression Language), but here is a quick summary.

  • The set tag. This tag defines an attribute. Use var to specify the attribute; use value to specify the value. Recall that in the jx library $attribute .beanProperty means that the system should call the getBeanProperty method of the object referenced by the attribute named attribute . You can also omit the value attribute and put the value between the start and end tags. For example:

    <jx:set var="name" value="$attribute.beanProperty"/> 
  • The expr tag. This tag returns a value. Use the value attribute to access existing attributes and bean properties with the jx library’s shorthand notation. If you use a default attribute, its value is used if exceptions are thrown when the system attempts to access the main value. For example:

    <jx:expr value="$attribute.beanProperty" default="value"/> 
  • The declare tag. This tag declares a scripting variable that can be accessed by JSP expressions and scriptlets. Use id to give the name of the variable; its initial value will be the attribute of the same name. Use type to give the fully qualified class name of the variable’s type. For example:

    <jx:declare id="name" type="package.Class"/> 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.21.175