Download PowerPoint 簡報

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Creating JSPs with the
Expression Language (EL)
鄧姚文
[email protected]
Outline
Understanding the Expression Language
 Using EL operators
 Incorporating functions with EL

2017/5/24
2
Introduction

Expression Language (EL)
◦ Depends less on Java
◦ Doesn’t use tags at all

盡量不要讓網頁設計人員看到程式碼
2017/5/24
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
2017/5/24
4
Using Implicit Variables in EL
Expressions
2017/5/24
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(
)}
2017/5/24
6
Using Implicit Variables in EL
Expressions
2017/5/24
7
Using EL Operators

EL operators can be divided into 4
categories:
◦
◦
◦
◦
◦
property/collection
access operators
arithmetic operators
relational operators
logical operators
2017/5/24
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

2017/5/24
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
2017/5/24
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 %

2017/5/24
11
EL Arithmetic Operators
字串自動轉數字
沒定義的變數當作 0
2017/5/24
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

2017/5/24
13
EL Relational and Logical Operators
${8.5 gt 4} evaluates to true
 ${(4 >= 9.2) || (1e2 <= 63)} evaluates to
false

2017/5/24
14
練習
用 EL 改寫 Hello 範例
 第一個頁面,讓使用者填寫姓名,選擇
性別
 第二個頁面,向使用者問好,顯示姓名
與稱謂

2017/5/24
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.
2017/5/24
16
Creating the Static Methods
2017/5/24
17
Creating a Tag Library Descriptor (TLD)
2017/5/24
18
Modifying the Deployment
Descriptor
2017/5/24
19
Accessing EL Functions within a JSP
2017/5/24
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

2017/5/24
21