EL 旨在簡化 JSP 頁面的數據訪問與邏輯判斷,替代繁瑣的 Scriptlet。語法結構:${expression}。JSP 2.0 起內置支持,無需額外配置。
| EL 關鍵字 | 對應 Java 運算符 | 說明 |
|---|---|---|
| div | / | 除法 |
| mod | % | 取餘 |
| eq | == | 等於 |
| ne | != | 不等於 |
| lt | < | 小於 |
| gt | > | 大於 |
| le | <= | 小於等於 |
| ge | >= | 大於等於 |
| and | && | 邏輯與 |
| or | || | 邏輯或 |
| not | ! | 邏輯非 |
| true / false | true / false | 布爾字面量 |
| null | null | 空值 |
| instanceof | instanceof | 類型判斷 |
| empty | - | 空值檢測(null、空串、空集合、空 Map、長度為 0 的數組) |
[] .
()
- (一元) ! empty
* / div % mod
+ - (二元)
< > <= >= lt gt le ge
== != eq ne
&& and
|| or
? : (三元運算符 ${A ? B : C})
EL 隱含對象均為 java.util.Map 類型,鍵為屬性名/參數名,值為對應對象:
| 隱含對象 | 作用域/來源 | 等價 API | 典型用法 |
|---|---|---|---|
| pageContext | PageContext | pageContext | ${pageContext.request.contextPath} |
| pageScope | Page | pageContext.getAttribute(name, PAGE_SCOPE) | ${pageScope.var} |
| requestScope | Request | request.getAttribute(name) | ${requestScope.user} |
| sessionScope | Session | session.getAttribute(name) | ${sessionScope.cart} |
| applicationScope | Application | application.getAttribute(name) | ${applicationScope.config} |
| param | 單值請求參數 | request.getParameter(name) | ${param.id} |
| paramValues | 多值請求參數 | request.getParameterValues(name) | ${paramValues.ids[0]} |
| header | 單值請求頭 | request.getHeader(name) | ${header['User-Agent']} |
| headerValues | 多值請求頭 | request.getHeaders(name) | ${headerValues.Accept} |
| cookie | Cookie 對象 | request.getCookies() | ${cookie.JSESSIONID.value} |
| initParam | 上下文初始化參數 | application.getInitParameter(name) | ${initParam.dbUrl} |
${username}:依次搜索 page → request → session → application,找到即返回,否則返回空串(不拋異常)${requestScope.username}:強制指定作用域,避免歧義${user.name} 等價 ${user["name"]},後者支持特殊字符鍵名(如 ${user["my-name"]})${list[0]}、${map.key}、${map["key.with.dot"]}package com.example.el;
public class StringFunctions {
public static String toUpper(String value) {
return value == null ? "" : value.toUpperCase();
}
public static String substring(String str, int begin, int end) {
return str.substring(begin, end);
}
}
/WEB-INF/tlds/functions.tld<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="https://java.sun.com/xml/ns/javaee"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://java.sun.com/xml/ns/javaee web-jsptaglibrary_2_1.xsd"
version="2.1">
<tlib-version>1.0</tlib-version>
<short-name>myfn</short-name>
<uri>https://example.com/functions</uri>
<function>
<name>toUpper</name>
<function-class>com.example.el.StringFunctions</function-class>
<function-signature>java.lang.String toUpper(java.lang.String)</function-signature>
</function>
<function>
<name>substring</name>
<function-class>com.example.el.StringFunctions</function-class>
<function-signature>java.lang.String substring(java.lang.String, int, int)</function-signature>
</function>
</taglib>
<%@ taglib prefix="fn" uri="https://example.com/functions" %>
<!-- 或直接引用 TLD 路徑:uri="/WEB-INF/tlds/functions.tld" -->
${fn:toUpper("hello world")} <!-- 輸出: HELLO WORLD -->
${fn:substring("abcdef", 1, 4)} <!-- 輸出: bcd -->
<%@ page isELIgnored="true" %> 或 web.xml 中 <el-ignored>true</el-ignored>${user.address.city} 若中間任一環為 null,輸出空串而非拋 NPE${"10" + 5} 結果為 15<fmt:formatDate> 或自定義函數fn:escapeXml() 或 JSTL <c:out>