1 / 21

Creating JSPs with the Expression Language (EL)

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 盡量不要讓網頁設計人員看到程式碼.

Download Presentation

Creating JSPs with the Expression Language (EL)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Creating JSPs with theExpression Language (EL) 鄧姚文 joseph.deng@gmail.com

  2. Outline • Understanding the Expression Language • Using EL operators • Incorporating functions with EL

  3. Introduction • Expression Language (EL) • Depends less on Java • Doesn’t use tags at all • 盡量不要讓網頁設計人員看到程式碼

  4. 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

  5. Using Implicit Variables in EL Expressions

  6. 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()}

  7. Using Implicit Variables in EL Expressions

  8. Using EL Operators • EL operators can be divided into 4 categories: • property/collection • access operators • arithmetic operators • relational operators • logical operators

  9. 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

  10. 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

  11. 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 %

  12. EL Arithmetic Operators 字串自動轉數字 沒定義的變數當作 0

  13. 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

  14. EL Relational and Logical Operators • ${8.5 gt 4} evaluates to true • ${(4 >= 9.2) || (1e2 <= 63)} evaluates to false

  15. 練習 • 用 EL 改寫 Hello 範例 • 第一個頁面,讓使用者填寫姓名,選擇性別 • 第二個頁面,向使用者問好,顯示姓名與稱謂

  16. 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.

  17. Creating the Static Methods

  18. Creating a Tag Library Descriptor (TLD)

  19. Modifying the Deployment Descriptor

  20. Accessing EL Functions within a JSP

  21. 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

More Related