|
|
||
|
|
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.
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
<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,
|
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
${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
${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.
|
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
|
|
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.
|
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
[ ]
()
(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
<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
<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
|
|
||
|
|