JSP Expression Language

You can also code this custom tag to make use of the JSP expression language. To do this, however, we must use a nonstandard class found in the org.apache. taglibs .standard.lang.support package. The ExpressionEvaluatorManager class gives us the ability to use the expression language in our custom tags. In the following code snippet, we see the code that would need to be added to make our tag process the expression:

 selIdEval  = (String)ExpressionEvaluatorManager.evaluate("map",                 // attribute name               selectedId,            // expression               java.lang.String.class, // expected type               this,                   // this tag handler               pageContext);           // the page context 

The selIdEval is a new private String variable that will hold the results of the evaluated expression. We can now update the idIsSelected method to use this value instead of the selectedId. It is important to remember that variables that are defined in scriptlets are not placed into one of the JSP scopes. We can do this using the set core tag with the following call:

 <c:set var="empId" value="180"/> 

This will place the empId into the pageContext. You could also use the implicit variable pageContext and call the setAttribute method to place it into the page scope. Since avoiding scriptlets in our JSPs is good programming practice, using the tag is a better option.

Note  

This is a very simple example and does not adequately show the power of the expression language. Ideally, JSPs are for presentation only and, generally , JavaBeans will be placed in one of the scopes by a servlet, so accessing them becomes the only concern for the JSP developer.

Accessing nested properties in the expression language (EL) is as simple as using the dot (.) operator. For example, if the employee data was stored in an EmployeeBean, then accessing the attributes could be done as follows :

 ${empBean.firstName} 

The above expression causes the getFirstName() method to be called on the empBean object located in one of the JSP scopes. If there are two EmployeeBean objects referred to by the empBean name, and one in the page scope and one in the request scope, you will only be able to retrieve the object in the pageScope. The various JSP scopes are searched in the order of the shortest-lived to the longest: page, request, session, then application. It is a good idea to make the names unique across the scopes. You can, however, refer to a specific scope by name, such as in the following example:

 ${pageScope.empBean.firstName} 

The pageScope element in the above expression is one of the implicit objects that the expression language provides. Table 14-7 describes each of the implicit objects that are available.

Table 14-7: Available Implicit Objects

Variable

Description

pageScope

Provides access to the objects that are stored in the JSPs scope

requestScope

Provides access to the objects that are stored in the JSP request scope

sessionScope

Provides access to the objects that are stored in the JSP session scope

applicationScope

Provides access to the objects that are stored in the JSP application scope

cookie

Provides access to cookies and their values

header

Provides access to headers and their values

headerValues

Provides access to header values

initParam

Provides access to the initialization parameters as defined in your web.xml file

param

Provides access to requested parameters

paramValues

Provides access to the request parameters that have multiple values

pageContext

Provides access to the pageContext and the objects contained in the page context such as the request, response, session, servlet config, and servlet context

The EL also has set of operators that are used the same way you would use them in Java. As with Java, the operators have precedence. Table 14-8 is a summary of the available operators and the list that follows shows the operator precedence.

Table 14-8: Available Operators

Type

Operators

Arithmetic

* / div % mod

Grouping

 

Identifier Access

. [ ]

Logical

&& (and) (or) ! (not) empty

Relational

== (eq) != (ne) < (lt) > (gt) <= (le) >= (ge)

Unary

-

This list ranks the operator precedence from highest to lowest , left to right:

  • [ ]

  • ()

  • (unary) not ! empty

  • / div % mod

  • + - (binary)

  • < > <= >= lt gt le ge

  • == != eq ne

  • && and

  • or

The following code example uses the JSTL choose, when, and otherwise tags and the expression language to create a web form that will determine if the supplied number is positive, negative, or zero. When the page is first displayed, the otherwise block is evaluated. Inside the otherwise tag s body, we have a JSTL if tag checking to make sure that the parameter is not null. If the numVal request parameter is not null, all of the when tags evaluate to false and The value is zero! message is displayed:

 <form name="negOrPos">   Please enter a number.<br>   <input type="text" name="numVal"            value="<c:out value="${param.numVal}"/>" >     <input type="submit"><br>   <c:choose>     <c:when test="${param.numVal > 0}">       The number is positive.     </c:when>     <c:when test="${param.numVal < 0}">     The number is negative.     </c:when>     <c:otherwise>       <c:if test=${param.numVal != null}>          The value is zero!       </c:if>     </c:otherwise>   </c:choose>   </form> 

You can concatenate expressions together inside JSTL tags and custom tags that make use of the EL. To accomplish this, place the JSP expressions next to each other as in the following example. The resulting output would look like: Smith, Tom. Notice that the comma interpreted literally.

 <c:out value="${firstName}, ${lastName}"/> 

One of the confusing things about the syntax is its use of quotes. In JSP and HTML tags, you can use either a single quote or tick (˜) or double quotes (). For example, the two following tags are equivalent:

<c:out value= ${firstName}, ${lastName} /><c:out value= ˜${firstName}, ${lastName} />

Many times, you will find yourself placing JSTL tags inside HTML tag attributes or other custom tags. For example, a form field is being set using the JSTL <c:out> tag. Notice that the quotes of the HTML tag are single quotes and the quotes used for the value attribute of the out tag are double quotes:

 <myTags:textInput name=fullName value=<c:out value="${name}"/>> 

When working the HTML tags, it is not as important to alternate the quotes, but if you use the same quote type, testInput, and out value attributes, your page will not compile. You must also escape quotes that appear inside your attribute values. Escaping a quote is similar to escaping quotes in Java: to escape a single quote use \ and to escape a double quote use \ .

Oracle ADF also uses the JSP EL in many of its tag libraries. Most experienced developers who first encounter the EL like it because it is simple to learn and makes JSP easier to read and maintain. The best resource for the details of the JSP expression language is the JavaServer Pages Standard Tag Library 1.0 specification. You can download it from the Sun Developer Network Site at http://java.sun.com.



Oracle Application Server 10g Web Development
Oracle Application Server 10g Web Development (Oracle Press)
ISBN: 0072255110
EAN: 2147483647
Year: 2004
Pages: 192

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net