Home

This Chapter
-Chapter 6: The Expression Language
-The Expression Language Syntax
-Accessing JavaBeans
-EL Implicit Objects
-Using Other EL Operators
-The EL in JSP 2.0
-Summary

Table of Contents
-Introduction
-Chapter 1: Model 2 and Struts
-Chapter 2: Input Validation with Action Forms
-Chapter 3: The HTML Tag Library
-Chapter 4: Input Validation and Data Conversion
-Chapter 5: The Validator Plugin
-Chapter 6: The Expression Language
-Chapter 7: JSTL
-Chapter 8: The Bean Tag Library
-Chapter 9: The Logic Tag Library
-Chapter 10: Struts-EL, Nested, selectLabel
-Chapter 11: Message Handling and Internationalization
-Chapter 12: The Tiles Framework
-Chapter 13: Securing Struts Applications
-Chapter 14: The Config Object
-Chapter 15: The Persistence Layer
-Chapter 16: Object Caching
-Chapter 17: File Upload and File Download
-Chapter 18: Paging and Sorting
-Chapter 19: Preventing Double Submits
-Chapter 20: Early HttpSession Invalidation
-Chapter 21: Decorating Request Objects
-Chapter 22: How Struts Works

Previous
Next

 

Using Other EL Operators

In addition to the . and [] operators, the EL also provides several other operators: arithmetic operators, relational operators, logical operators, the conditional operator, and the empty operator. Using these operators, you can perform various operations. However, because the aim of the EL is to facilitate the authoring of script-free JSPs, these EL operators are of limited use, except for the conditional operator.

The EL operators are given in the following subsections.

Arithmetic Operators

There are five arithmetic operators:

The division and remainder operators have two forms, to be consistent with XPath and ECMAScript.

Note that an EL expression is evaluated from the highest to the lowest precedence, and then from left to right. The following are the arithmetic operators in the decreasing lower precedence:

* / div % mod
+ -

This means that *, /, div, %, and mod operators have the same level of precedence, and + has the same precedence as - , but lower than the first group. Therefore, the expression

${1+2*3}

results in 7 and not 6.

Relational Operators

The following is the list of relational operators:

For instance, the expression ${3==4} returns false, and ${“b”<”d”} returns true.

Logical Operators

Here is the list of logical operators:

The Conditional Operator

The EL conditional operator has the following syntax:

${statement? A:B}

If statement evaluates to true, the output of the expression is A. Otherwise, the output is B.

For example, you can use the following EL expression to test whether the HttpSession object contains the attribute called loggedIn. If the attribute is found, the string “You have logged in” is displayed. Otherwise, “You have not logged in” is displayed.

${(sessionScope.loggedIn==null)? "You have not logged in" :
  "You have logged in"}

The empty Operator

The empty operator is used to examine whether a value is null or empty. The following is an example of the use of the empty operator:

${empty X}

If X is null or if X is a zero-length string, the expression returns true. It also returns true if X is an empty Map, an empty array, or an empty collection. Otherwise, it returns false.

Previous
Next