PowerPoint 簡報

Download Report

Transcript PowerPoint 簡報

Creating JSPs with the
Expression Language (EL)
鄧姚文
[email protected]
Outline
Understanding the Expression Language
 Using EL operators
 Incorporating functions with EL

2015/7/17
2
Introduction

Expression Language (EL)
◦ Depends less on Java
◦ Doesn’t use tags at all

盡量不要讓網頁設計人員看到程式碼
2015/7/17
3
Understanding The Expression
Language

All EL expressions begin with “${“ and
end with “}”
◦ JSP script expression
The outside temperature is <%= temp %>
degrees.
◦ EL
The outside temperature is ${temp} degrees.

EL expressions can’t use variables
declared in scripts
2015/7/17
4
Using Implicit Variables in EL
Expressions
2015/7/17
5
Using Implicit Variables in EL
Expressions

Display the bufferSize of the page’s
JSPWriter
${pageContext.out.bufferSize}

Retrieve the request’s HTTP method
${pageContext.request.method}

EL restrains you from invoking Java
methods, you can’t use an expression like
${pageContext.request.getMethod(
)}
2015/7/17
6
Using Implicit Variables in EL
Expressions
2015/7/17
7
Using EL Operators

EL operators can be divided into 4
categories:
◦
◦
◦
◦
◦
property/collection
access operators
arithmetic operators
relational operators
logical operators
2015/7/17
8
EL Operators for Property and
Collection Access
a.b — Returns the property of a
associated with the identifier, b
 a[b] — Returns the value of a associated
with the key or index, b
 If b is a String EL treats them
interchangeably

2015/7/17
9
EL Operators for Property and
Collection Access

The following produce the same result
◦ ${header["host"]}
◦ ${header['host']}
◦ ${header.host}

In this case, header is a Map, so the
header.get("host") method is invoked to
display the expression’s result
2015/7/17
10
EL Arithmetic Operators
Integer and BigInteger values for fixedpoint numbers
 Double and BigDecimal values for
floating-point numbers
 Addition: +
 Subtraction: –
 Multiplication: *
 Division: div and /
 Modulo division: mod and %

2015/7/17
11
EL Arithmetic Operators
字串自動轉數字
沒定義的變數當作 0
2015/7/17
12
EL Relational and Logical Operators
Equality: == and eq
 Non-equality: != and ne
 Less than: < and lt
 Greater than: > and gt
 Less than or equal: <= and le
 Greater than or equal: >= and ge
 Logical conjunction: && and and
 Logical disjunction: || and or
 Logical inversion: ! and not

2015/7/17
13
EL Relational and Logical Operators
${8.5 gt 4} evaluates to true
 ${(4 >= 9.2) || (1e2 <= 63)} evaluates to
false

2015/7/17
14
練習
用 EL 改寫 Hello 範例
 第一個頁面,讓使用者填寫姓名,選擇
性別
 第二個頁面,向使用者問好,顯示姓名
與稱謂

2015/7/17
15
Incorporation Functions with EL





The process of inserting an EL function into a JSP
involves creating or modifying four files:
Method class (*.java)—Contains the Java methods
that you want to use in your JSP.
Tag library descriptor (*.tld)—Matches each Java
method to an XML function name.
Deployment descriptor (web.xml)—Matches the
TLD to a tag library URI. (Note: Changing this file
is optional, but recommended.)
JavaServer Page (*.jsp)—Uses the tag library URI
and function name to invoke the method.
2015/7/17
16
Creating the Static Methods
2015/7/17
17
Creating a Tag Library Descriptor (TLD)
2015/7/17
18
Modifying the Deployment
Descriptor
2015/7/17
19
Accessing EL Functions within a JSP
2015/7/17
20
SUMMARY
Primary Goal of EL: to remove Java from JSP
development
 The Expression Language provides
essentially the same constructs for property
access, collection access, arithmetic, logic,
and relational comparisons as C or Java.
 Property access and collection access are
essentially the same thing in EL
 EL numbers must be of the Integer,
BigInteger, Double, or BigDecimal data types

2015/7/17
21