1、Unlike the earlier chapters, this chapter provides a more in-depth exploration of a cornerstone element of the JSP standard: JSPs Expression Language, or EL for short.In this chapter, you will discover the following: Why EL is an indispensable part of JSP 2.0 How to use EL named variables How to per
2、form type conversion and coercion in EL How to handle null values in EL How to work with arithmetic, logical, comparison, and other operators in EL The different ways of accessing members of a collection in EL How to access properties of objects in EL How to access nested object properties in EL How
3、 to use implicit objects in EL How to define and access functions in EL How to use a namespace when referencing EL functionsThis chapter covers a lot of ground, and includes five separate examples. By the end of the chapter, you should be very familiar with EL, and be comfortable using it in your ow
4、n applications.EL and Its Vital Role in JSPEL was not part of the JSP standard prior to 2.0. One of the main reasons for creating EL was to ensure that presentation-level JSP pages could be created without relying on scripting elements. Chapter 3, “JSP Basics 2: Generalized Templating and Server Scr
5、ipting,” covered the use of scripting elements (typically code written in Java) that are embedded within a JSP page.The need for scripting elements within JSP is typically driven by the requirements of an application.Major application requirements that demand the use of scripting elements include th
6、e following: To provide control-flow for JSP execution To set, and subsequently access, variables that are local to the JSP page To render a value from a complex expression that involves Java objects To access properties of an arbitrary Java object To call methods of JavaBeans or other Java objectsU
7、nfortunately, experience has shown that the use of scripting elements in JSPs makes large projects diffi-cult to maintain over the long term. It also encourages programming practices that may tightly couple the presentation (user interface) of an application to its business logic, reducing its flexi
8、bility and scala-bility. This is a highly undesirable practice in the creation of Web applications and services. Ideally, JSPs should be created free of scripting elements if at all possible.In order to create a JSP version that can work completely free of scripting elements, it is essential that al
9、l five of the application requirements can be satisfied without the use of embedded Java coding. JSP 2.0 satisfies these requirements. The first two items are handled by JSTL (explored in detail in Chapter 6, “JSP Tag Libraries and JSTL”), while the last three requirements are handled by EL.EL is in
10、dependent of JSP 2.0. Despite the vital role of EL in JSP 2.0, the use of EL is not exclusive to JSP containers. The EL parser is also not tightly integrated into the JSP parser, but can be detached and reused for other purposes. In fact, EL is incorporated into both Java Server Faces (JSF, a server
11、-side GUI construction kit technology; see Chapter 21, “JavaServer Faces,” for more information), and JSTL. Therefore, becoming familiar with EL will save you time when exploring these other technologies.EL Named VariablesWhen working with EL (and JSTL), you will frequently work with named variables
12、. Named variables in EL refer to attributes attached to JSP scoping objects. For example, the following EL expression will ren-der the value of the named variable called bearCount:The EL parser will search for the attribute through the various JSP scoping objects in the following order:1Page2.Reques
13、tSession (if currently valid)ApplicationIn practice, this is equivalent to a call to the Java PageContext.findAttribute() method. If you are maintaining legacy JSP code with embedded Java, you may see this call. You can consult the JSP Javadocs (API documentation) to see how this method works.Figure
14、 5-1 illustrates the search for an EL named variable.Figure 5-1: Order of search for an EL named variableIn Figure 5-1, you can see the search order for an EL named variable: First the set of EL implicit objects is searched for a matching name (EL implicit objects are covered later in this chapter),
15、 and then the four scopes. If the named variable is not found, some action may create it.If the attribute with the specified name cannot be located in any scope, a null value is returned. The null value will render as a “” (empty string) in the output, and will not trigger any error.Applying ELAs th
16、e name implies, the Expression Language works with expressions. It is a programming language used in the construction of expressions. All EL expressions are enclosed in the $. notation. The expression is evaluated before the rest of the JSP is evaluated. You can place EL expressions in two places wi
17、thin a JSP: Inline with template data Within attributes of JSP actions, standard or customUsing EL expressions inline with template dataThe most typical use of EL expressions is to render a textual string inline with template data. For example, consider the following JSP fragment:There are $5 + 1 be
18、ars in the cave.In this case, the EL expression is placed inline with the HTML template data. At JSP processing time, the expression $5+1 will evaluate to 6, and be placed at the same position as the original expression. This will result in the following HTML:There are 6 bears in the cave. 100000”Ri
19、ch cousin!br/c:ifIn this case, the EL expression $salary 100000 is used to conditionally render the body of the JSTL tag. It is placed in the test attribute of the custom JSTL action. This will cause the EL expression to be evaluated before the JSTL custom action is evaluated. The HTML will be rende
20、red only if the named variable called salary is greater in value than 100,000.For example, consider the following JSP fragment featuring the JSTL tag:set var=”datetime” val=$dateNow - $timeNow/If the dateNow named variable contains the string “May 1, 2005” and timeNow contains “11:00 AM”, then the r
21、esulting datetime named variable will contain “May 1, 2005 - 11:00 AM”.As shown in the preceding section, an EL expression can be an arithmetic expression, or an expression featuring a comparison. The following Try It Out section explores some of these EL expressions.To try out the first example, de
22、ploy ch05.war to your Tomcat 5 server. Review Chapter 1 if you do not remember how to deploy a WAR file.Access the following URL using your browser to see the examples output:http:/localhost:8080/ch05/example1/You should see something similar to what is shown in Figure 5-2.Figure 5-2: Output result
23、of EL expressionsThe output in Figure 5-2 is the result of a collection of different EL expressions. The following How It Works section examines the EL expressions used to generate this output.How It WorksThe JSP used to generate the output shown in Figure 5-2 is located at Tomcat Installation Note
24、that operator precedence has caused the multiplication and division to be executed first. Furthermore, the calculation has caused a decimal to appear in the output. You can use the numeric formatting tags in JSTL to eliminate this. For example, the following JSP fragment in index.jsp renders the sam
25、e result with-out the decimal point.The formatNumber JSTL tag is used to format the numerical output, using a pattern that specifies no decimal point should be displayed. This results in the following output.Comparison operatorsEL supports the entire range of comparison operators found in most progr
26、amming languages. Each of the operators has a symbolic form and an abbreviated textual form. The following table enumerates the available operators.Note that in the previous comparison EL always attempts to convert the type of the data being com-pared “in the right way.” That is, the character data
27、is converted to numeric value before being com-pared, except in the case where two characters are compared.In the previous example, the empty string “” is considered empty while any other string value is not. A nonexistent variable, Junk, is given a null value in EL. This null value is considered em
28、pty when tested with the empty operator. On the other hand, the guess variable exists (used earlier) and therefore is not empty. In other words, depending on the data type of the operand, the empty operator can also be used to test for the conditions shown in the following table.The named variable S
29、trVar is initially set to the string true. This is a variable of type String. However, because the test attribute of the JSTL tag requires a Boolean expression, it is automatically con-verted to a Boolean by EL before use. This causes the printing of the “equal!” message.Finally, the comparison operator is being used on the special null value. It is shown