12.6. Evaluating Items Conditionally

JSTL provides two main tags for performing conditional evaluation: if and choose. The if tag evaluates its body only when a specified test condition is true. There is no “else” condition—the content is either included or omitted with no alternative content. The following list summarizes the use of if; the following subsections give details and examples.

  • The if tag in the 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 %>"> 
      HTML or JSP content. 
    </jr:if> 
  • The if tag in the 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 (==, !=, <, >, <=, or >=). For example:

    <jx:if test="$attribute.beanProperty == 'value'"> 
      HTML or JSP content. 
    </jx:if> 

Whereas the if tag provides a single option that is either evaluated or ignored, the choose tag lets you specify several different options with nested when tags. The content of the first option whose test condition is true is evaluated. You can use the otherwise tag to provide default content when no test is true. The following list summarizes the use of choose; the following subsections give details and examples.

  • The choose tag in the 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 %>">Content1</jr:when> 
      <jr:when test="<% expression2 %>">Content2</jr:when> 
      ... 
      <jr:when test="<% expressionN %>">ContentN</jr:when> 
      <jr:otherwise>Default Content</jr:otherwise> 
    </jr:choose> 
  • The choose tag in the jx library. Use the same basic syntax as with the jr library, but specify the tests by accessing an existing attribute and comparing it to another value using one of a small set of relational operators (==, !=, <, >, <=, or >=). For example:

    <jx:choose> 
      <jx:when test="$att.prop1 == 'val1'">Content1</jx:when> 
      <jx:when test="$att.prop2 == 'val2'">Content2</jx:when> 
      ... 
      <jx:when test="$att.propN == 'valN'">ContentN</jx:when> 
      <jx:otherwise>Default Content</jx:otherwise> 
    </jx:choose> 

The if Tag

With the if tag, you simply specify a condition with the test attribute. If it is true (i.e., is the boolean value true or the Boolean value Boolean.TRUE), the content between the start and end tags is evaluated. Otherwise, the tag body is ignored.

For example, the following jr library code prints a bulleted list of the numbers from 1 to 10. If the number is greater than 7, a notation to that effect is placed after the number.

<UL> 
<jr:forEach var="i" begin="1" end="10"> 
  <% Integer i = (Integer)pageContext.getAttribute("i"); %> 
  <LI><%= i %> 
      <jr:if test="<%= i.intValue() > 7 %>">
							(greater than 7)
							</jr:if> 
</jr:forEach> 
</UL> 

The jx library follows a similar procedure. The difference is that the test attribute uses existing attributes and one of the following predefined comparisons: ==, !=, <, >, <=, or >=. It is important to realize that these operators do not behave the same as their Java equivalents. In particular, all of the operators support String arguments. For example, the == operator uses the equals method when both arguments are of type String but does not generate an exception when either argument is null. Similarly, >, <, etc., can be applied to strings as well as numbers. If the arguments are both strings, the result is that of the compareTo method of the String class.

Core Warning

The relational operators of the jx library work differently than they do in standard Java code. In particular, all of them can be used to compare String objects.


For example, the following jx code generates the same list as the jr code just shown.

<UL> 
<jx:forEach var="i" begin="1" end="10"> 
  <LI><jx:expr value="$i"/> 
      <jx:if test="$i > 7">
							(greater than 7)
							</jx:if> 
</jx:forEach> 
</UL> 

Listing 12.29 shows a JSP page that uses the jr code; Figure 12-20 shows the result. Listing 12.30 shows a JSP page that uses the jx code; Figure 12-21 shows the result.

Figure 12-20. Result of if-jr.jsp.


Figure 12-21. Result of if-jx.jsp.


Listing 12.29. if-jr.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE>If: "jr" Version</TITLE> 
<LINK REL=STYLESHEET 
      HREF="../styles.css" 
      TYPE="text/css"> 
</HEAD> 

<BODY> 
<TABLE BORDER=5 ALIGN="CENTER"> 
  <TR><TH CLASS="TITLE"> 
      If: "jr" Version 
</TABLE> 
<P> 
<%@ taglib uri="http://java.sun.com/jsptl/ea/jr" prefix="jr" %> 
<UL> 
<jr:forEach var="i" begin="1" end="10"> 
  <% Integer i = (Integer)pageContext.getAttribute("i"); %> 
  <LI><%= i %> 
      <jr:if test="<%= i.intValue() > 7 %>"> 
      (greater than 7) 
      </jr:if> 
</jr:forEach> 
</UL> 
</BODY> 
</HTML> 

Listing 12.30. if-jx.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE>If: "jx" Version</TITLE> 
<LINK REL=STYLESHEET 
      HREF="../styles.css" 
      TYPE="text/css"> 
</HEAD> 

<BODY> 
<TABLE BORDER=5 ALIGN="CENTER"> 
  <TR><TH CLASS="TITLE"> 
      If: "jx" Version 
</TABLE> 
<P> 
<%@ taglib uri="http://java.sun.com/jsptl/ea/jx" prefix="jx" %> 
<UL> 
<jx:forEach var="i" begin="1" end="10"> 
  <LI><jx:expr value="$i"/> 
      <jx:if test="$i > 7"> 
      (greater than 7) 
      </jx:if> 
</jx:forEach> 
</UL> 
</BODY> 
</HTML> 

The choose, when, and otherwise Tags

With the choose tag, you supply a set of possible results by using nested when tags. Each when tag uses the test attribute in the same manner as with the if tag. The body of the first when tag whose condition is true (i.e., is the boolean value true or the Boolean value Boolean.TRUE) is evaluated. If no when tag is true and there is an otherwise tag, its body is evaluated.

For example, the following jr code generates a bulleted list of numbers. If the number is less than four, it is marked as “small.” If the number is greater than or equal to four but less than eight, it is marked as “medium.” If the number is greater than eight, it is marked as “large.”

<UL> 
<jr:forEach var="i" begin="1" end="10"> 
  <% Integer i = (Integer)pageContext.getAttribute("i"); %> 
  <LI><%= i %> 
      <jr:choose>
							<jr:when test="<%= i.intValue() < 4 %>">
							(small)
							</jr:when>
							<jr:when test="<%= i.intValue() < 8 %>">
							(medium)
							</jr:when>
							<jr:otherwise>
							(large)
							</jr:otherwise>
							</jr:choose> 
</jr:forEach> 
</UL> 

Here is the jx equivalent: shorthand tests are used instead of explicit JSP expressions, yielding a more concise and readable result.

<UL> 
<jx:forEach var="i" begin="1" end="10"> 
  <LI><jx:expr value="$i"/> 
      <jx:choose>
							<jx:when test="$i < 4">
							(small)
							</jx:when>
							<jx:when test="$i < 8">
							(medium)
							</jx:when>
							<jx:otherwise>
							(large)
							</jx:otherwise>
							</jx:choose> 
</jx:forEach> 
</UL> 

Listing 12.31 shows a JSP page that uses the jr code; Figure 12-22 shows the result. Listing 12.32 shows a JSP page that uses the jx code; Figure 12-23 shows the result.

Figure 12-22. Result of choose-jr.jsp.


Figure 12-23. Result of choose-jx.jsp.


Listing 12.31. choose-jr.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE>Choose: "jr" Version</TITLE> 
<LINK REL=STYLESHEET 
      HREF="../styles.css" 
      TYPE="text/css"> 
</HEAD> 

<BODY> 
<TABLE BORDER=5 ALIGN="CENTER"> 
  <TR><TH CLASS="TITLE"> 
      Choose: "jr" Version 
</TABLE> 
<P> 
<%@ taglib uri="http://java.sun.com/jsptl/ea/jr" prefix="jr" %> 
<UL> 
<jr:forEach var="i" begin="1" end="10"> 
  <% Integer i = (Integer)pageContext.getAttribute("i"); %> 
  <LI><%= i %> 
      <jr:choose>
							<jr:when test="<%= i.intValue() < 4 %>"> 
          (small) 
        </jr:when>
							<jr:when test="<%= i.intValue() < 8 %>"> 
          (medium) 
        </jr:when>
							<jr:otherwise> 
          (large) 
        </jr:otherwise>
							</jr:choose> 
</jr:forEach> 
</UL> 
</BODY> 
</HTML> 

Listing 12.32. choose-jx.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE>Choose: "jx" Version</TITLE> 
<LINK REL=STYLESHEET 
      HREF="../styles.css" 
      TYPE="text/css"> 
</HEAD> 

<BODY> 
<TABLE BORDER=5 ALIGN="CENTER"> 
  <TR><TH CLASS="TITLE"> 
      Choose: "jx" Version 
</TABLE> 
<P> 
<%@ taglib uri="http://java.sun.com/jsptl/ea/jx" prefix="jx" %> 
<UL> 
<jx:forEach var="i" begin="1" end="10"> 
  <LI><jx:expr value="$i"/> 
      <jx:choose>
							<jx:when test="$i < 4"> 
          (small) 
        </jx:when>
							<jx:when test="$i < 8"> 
          (medium) 
        </jx:when>
							<jx:otherwise> 
          (large) 
        </jx:otherwise>
							</jx:choose> 
</jx:forEach> 
</UL> 
</BODY> 
</HTML> 

The Use of the set Tag with the choose Tag

In the previous example, the jx version of choose yielded more succinct code than did the jr version because the tests were performed on an attribute that was set by the forEach tag. When the tests are based on explicit computations, however, the jx code is more complicated.

For example, here is some jr code that prints out the results of ten coin tosses.

<UL> 
<jr:forEach var="i" begin="1" end="10"> 
  <% double d = Math.random(); %> 
  <LI><jr:choose> 
        <jr:when test="<%= d < 0.49 %>"> 
          Heads. 
        </jr:when> 
        <jr:when test="<%= d < 0.98 %>"> 
          Tails. 
        </jr:when> 
        <jr:otherwise> 
          <B>Coin landed on edge!</B> 
        </jr:otherwise> 
      </jr:choose> 
</jr:forEach> 
</UL> 

How would you perform the equivalent tasks in the jx library? The test attribute of the when tag does not accept a JSP expression. Sure, you could use a scriptlet to explicitly store the result of the call to Math.random in a PageContext attribute, but that results in so much scripting code that it negates any advantage that the jx library might have had. Instead, you can use the set tag to evaluate a JSP expression and automatically store the result in a PageContext attribute. The set tag is described in Section 12.7, but the gist is that you can supply a value either with the value attribute or by enclosing the value between the start and end tags of set. The result is placed in the PageContext attribute named by the var attribute of set. Given this behavior of set, the following jx code yields the same result as the previous jr example.

<UL> 
<jx:forEach var="i" begin="1" end="10"> 
  <jx:set var="d"><%= Math.random() %></jx:set> 
  <LI><jx:choose> 
        <jx:when test="$d < '0.49'"> 
          Heads. 
        </jx:when> 
        <jx:when test="$d < '0.98'"> 
          Tails. 
        </jx:when> 
        <jx:otherwise> 
          <B>Coin landed on edge!</B> 
        </jx:otherwise> 
      </jx:choose> 
</jx:forEach> 
</UL> 

Listing 12.33 shows a JSP page that uses the jr code; Figure 12-24 shows the result. Listing 12.34 shows a JSP page that uses the jx code; Figure 12-25 shows the result.

Figure 12-24. Result of coin-toss-jr.jsp.


Figure 12-25. Result of coin-toss-jx.jsp.


Listing 12.33. coin-toss-jr.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE>Coin Toss: "jr" Version</TITLE> 
<LINK REL=STYLESHEET 
      HREF="../styles.css" 
      TYPE="text/css"> 
</HEAD> 

<BODY> 
<TABLE BORDER=5 ALIGN="CENTER"> 
  <TR><TH CLASS="TITLE"> 
      Coin Toss: "jr" Version 
</TABLE> 
<P> 
<%@ taglib uri="http://java.sun.com/jsptl/ea/jr" prefix="jr" %> 
<UL> 
<jr:forEach var="i" begin="1" end="10"> 
  <% double d = Math.random(); %> 
  <LI><jr:choose>
							<jr:when test="<%= d < 0.49 %>"> 
          Heads. 
        </jr:when>
							<jr:when test="<%= d < 0.98 %>"> 
          Tails. 
        </jr:when>
							<jr:otherwise> 
          <B>Coin landed on edge!</B> 
        </jr:otherwise>
							</jr:choose> 
</jr:forEach> 
</UL> 
</BODY> 
</HTML> 

Listing 12.34. coin-toss-jx.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE>Coin Toss: "jx" Version</TITLE> 
<LINK REL=STYLESHEET 
      HREF="../styles.css" 
      TYPE="text/css"> 
</HEAD> 

<BODY> 
<TABLE BORDER=5 ALIGN="CENTER"> 
  <TR><TH CLASS="TITLE"> 
      Coin Toss: "jx" Version 
</TABLE> 
<P> 
<%@ taglib uri="http://java.sun.com/jsptl/ea/jx" prefix="jx" %> 
<UL> 
<jx:forEach var="i" begin="1" end="10"> 
  <jx:set var="d"><%= Math.random() %></jx:set> 
  <LI><jx:choose>
							<jx:when test="$d < '0.49'"> 
          Heads. 
        </jx:when>
							<jx:when test="$d < '0.98'"> 
          Tails. 
        </jx:when>
							<jx:otherwise> 
          <B>Coin landed on edge!</B> 
        </jx:otherwise>
							</jx:choose> 
</jx:forEach> 
</UL> 
</BODY> 
</HTML> 

..................Content has been hidden....................

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