12.7. Using the Expression Language

The set, expr, and declare tags let you use shorthand notation to 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 and examples are provided in the following subsections, but here is a quick summary.

  • set. This tag defines a PageContext attribute. Use var to specify the name of the attribute; use value to specify its value. When using the value attribute, 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"/> 
  • expr. 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"/> 
  • declare. This tag provides a bridge between the jx library and scripting code. It declares a scripting variable (i.e., a local variable in _jspService) 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"/> 

The set Tag

The set tag defines a PageContext attribute with a name that is specified with the var attribute. The set tag has two main purposes.

First, set can be used to evaluate a JSP expression and store its result in an attribute. This use of set applies to situations in which you cannot entirely avoid explicit scripting expressions but you still want to primarily use the jx library. With this usage, the value is placed inside the tag body. For example, in the coin-toss example of the previous section, the following code was used to store the result of Math.random in a PageContext attribute named d.

<jx:set var="d"><%= Math.random() %></jx:set> 

Second, set can be used to store intermediate results. For example, the results of long and complicated expressions can be stored in meaningfully named attributes, thus simplifying the expr tags that use the values. With this usage, the value is specified with the value attribute. For example, the following code uses set to establish the count, index, and letter attributes.

<UL> 
<jx:forEach status="status" begin="1" step="2" 
    items="a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"> 
  <jx:set var="count" value="$status.count"/>
							<jx:set var="index" value="$status.index"/>
							<jx:set var="letter" value="$status.current"/> 
  <LI>Item <jx:expr value="$count"/> 
      <UL> 
        <LI>Index: <jx:expr value="$index"/> 
        <LI>Letter: <jx:expr value="$letter"/> 
      </UL> 
</jx:forEach> 
</UL> 

Listing 12.35 shows a JSP page that uses this set code; Figure 12-26 shows the result.

Figure 12-26. Result of set.jsp.


Listing 12.35. set.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE>Setting PageContext Attributes</TITLE> 
<LINK REL=STYLESHEET 
      HREF="../styles.css" 
      TYPE="text/css"> 
</HEAD> 

<BODY> 
<TABLE BORDER=5 ALIGN="CENTER"> 
  <TR><TH CLASS="TITLE"> 
      Setting PageContext Attributes 
</TABLE> 
<P> 
<%@ taglib uri="http://java.sun.com/jsptl/ea/jx" prefix="jx" %> 
<UL> 
<jx:forEach status="status" begin="1" step="2" 
    items="a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"> 
  <jx:set var="count" value="$status.count"/>
							<jx:set var="index" value="$status.index"/>
							<jx:set var="letter" value="$status.current"/> 
  <LI>Item <jx:expr value="$count"/> 
      <UL> 
        <LI>Index: <jx:expr value="$index"/> 
        <LI>Letter: <jx:expr value="$letter"/> 
      </UL> 
</jx:forEach> 
</UL> 
</BODY> 
</HTML> 

The expr Tag

The expr tag normally returns the value specified in its value attribute; I use expr in virtually every jx example in this chapter. However, what happens if value specifies an attribute that does not exist? The result is an error, not simply null. In most cases, this behavior is not a problem: you know the attribute exists because it is specified by an enclosing forEach or forTokens tag, an earlier set tag, or the servlet that forwarded the request to the JSP page. However, suppose that you use the param scoping qualifier to read request parameters with the $param:paramName shorthand format? In such a case, you cannot be sure whether the request parameter exists or not: it is supplied by the end user. Similarly, if you use the header scoping qualifier to read a request header, you cannot know in advance whether or not the specified header will exist. In both cases, you get an error, not null, if you try to use the value when it is unavailable.

To handle this problem, the expr tag has a default attribute. Its value is used if an exception occurs when the system tries to compute the value given in the value attribute. For example, the following code uses the alertLevel request parameter to decide what heading to generate. If the alertLevel parameter is not in the request, a default heading is generated. If expr had not been used and $param:alertLevel was used directly in the tests, an error would have resulted when alertLevel was not a request parameter.

							<jx:set var="level">
							<jx:expr value="$param:alertLevel" default="low"/>
							</jx:set> 
<jx:choose> 
  <jx:when test="$level == 'high'"><H1>Code Red!</H1></jx:when> 
  <jx:when test="$level == 'medium'"><H1>Code Blue</H1></jx:when> 
  <jx:otherwise><H2>Code White</H2></jx:otherwise> 
</jx:choose> 

Listing 12.36 shows a JSP page that uses this expr code; Figures 12-27 through 12-29 show the results when the alertLevel request parameter is high, medium, and missing, respectively.

Figure 12-27. Result of expr.jsp when invoked with an alertLevel request parameter of high.


Figure 12-28. Result of expr.jsp when invoked with an alertLevel request parameter of medium.


Figure 12-29. Result of expr.jsp when invoked with no request parameters.


Listing 12.36. expr.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE>Expressions with Defaults</TITLE> 
<LINK REL=STYLESHEET 
      HREF="../styles.css" 
      TYPE="text/css"> 
</HEAD> 
<BODY> 
<TABLE BORDER=5 ALIGN="CENTER"> 
  <TR><TH CLASS="TITLE"> 
      Expressions with Defaults 
</TABLE> 
<%@ taglib uri="http://java.sun.com/jsptl/ea/jx" prefix="jx" %> 
<jx:set var="level"> 
  <jx:expr value="$param:alertLevel" default="low"/> 
</jx:set> 
<jx:choose> 
  <jx:when test="$level == 'high'"><H1>Code Red!</H1></jx:when> 
  <jx:when test="$level == 'medium'"><H1>Code Blue</H1></jx:when> 
  <jx:otherwise><H2>Code White</H2></jx:otherwise> 
</jx:choose> 
</BODY> 
</HTML> 

The declare Tag

The jx library is preferred by developers who want to minimize the amount of explicit scripting code in their JSP pages. In fact, JSTL provides a TagLibraryValidator (see Section 11.3) called ScriptFreeTLV that you can use to verify that certain pages contain no scripting code whatsoever.

However, despite the ability to access attributes and bean properties with shorthand notation, you cannot always avoid the use of scripting elements. When scripting code is necessary, some developers prefer to simply switch to the jr library. Others prefer to stick with jx but keep the scripting code to a minimum. The declare tag is designed for this latter situation. It copies the value of an existing attribute into a scripting variable (i.e., a local variable in the _jspService method) that has the same name. This variable can then be accessed by scripting code.

For example, Listing 12.37 shows a very short JSP page that reads the url request parameter and redirects the user to the specified page. It uses the jx library to take advantage of two capabilities: the param scoping qualifier to simplify access to the request parameter and the default attribute of expr to simplify the situation when the url parameter is missing. However, since the call to sendRedirect requires explicit scripting code, the declare tag copies the value from the url PageContext attribute into a String variable named url. Figure 12-30 shows the result when the page is accessed without a url request parameter. Figure 12-31 shows the result when url is part of the request data.

Figure 12-30. Result of declare.jsp when no url request parameter is supplied (http://localhost/jsptl/expressions/declare.jsp).


Figure 12-31. Result of declare.jsp when a url request parameter is supplied (http://localhost/jsptl/expressions/declare.jsp?url=http://courses.coreservlets.com).


Listing 12.37. declare.jsp
<%@ taglib uri="http://java.sun.com/jsptl/ea/jx" prefix="jx" %> 
<jx:set var="url"> 
  <jx:expr value="$param:url" 
           default="http://java.sun.com/products/jsp/"/> 
</jx:set> 
<jx:declare id="url" type="java.lang.String"/> 
<% response.sendRedirect(url); %> 

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

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