210 likes | 352 Views
Creating JSPs with the Expression Language (EL). 鄧姚文 joseph.deng@gmail.com. Outline. Understanding the Expression Language Using EL operators Incorporating functions with EL. Introduction. Expression Language (EL) Depends less on Java Doesn’t use tags at all 盡量不要讓網頁設計人員看到程式碼.
E N D
Creating JSPs with theExpression Language (EL) 鄧姚文 joseph.deng@gmail.com
Outline • Understanding the Expression Language • Using EL operators • Incorporating functions with EL
Introduction • Expression Language (EL) • Depends less on Java • Doesn’t use tags at all • 盡量不要讓網頁設計人員看到程式碼
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
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()}
Using EL Operators • EL operators can be divided into 4 categories: • property/collection • access operators • arithmetic operators • relational operators • logical operators
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
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
EL Arithmetic Operators • Integer and BigInteger values for fixed-point numbers • Double and BigDecimal values for floating-point numbers • Addition: + • Subtraction: – • Multiplication: * • Division: div and / • Modulo division: mod and %
EL Arithmetic Operators 字串自動轉數字 沒定義的變數當作 0
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
EL Relational and Logical Operators • ${8.5 gt 4} evaluates to true • ${(4 >= 9.2) || (1e2 <= 63)} evaluates to false
練習 • 用 EL 改寫 Hello 範例 • 第一個頁面,讓使用者填寫姓名,選擇性別 • 第二個頁面,向使用者問好,顯示姓名與稱謂
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.
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