<%@ page isELIgnored="true" %> <!-- 禁用 EL,預設 false (啟用) -->
JSP 2.0+ 容器原生支援 EL,無需 JSTL 標籤庫即可在頁面任意位置使用。
所有 EL 以 ${ 開始、} 結束:
${sessionScope.user.sex}
${sessionScope.user["sex"]} <!-- 等價 -->
${sessionScope.shoppingCart[0].price} <!-- 陣列/List 索引 -->
${user["My-Name"]} <!-- 屬性名含特殊字符必須用 [] -->
${sessionScope.user[data]} <!-- data 為變數,動態取值 -->
| 類別 | 運算符 | 說明 |
|---|---|---|
| 存取 | .、[] | 屬性/Map Key/索引存取,可混合 |
| 算術 | + - * / div % mod | div/mod 為除/取餘別名,避免與路徑衝突 |
| 比較 | == eq != ne < lt > gt <= le >= ge | 字母別名避免 XML 轉義 |
| 邏輯 | && and || or ! not | |
| 三元 | condition ? yes : no | |
| 空值 | empty | empty obj 判斷 null、空字串、空集合、空 Map |
| EL 名稱 | 對應 API | 用途 |
|---|---|---|
pageScope | PageContext.getAttribute() | 頁面級暫存 |
requestScope | HttpServletRequest.getAttribute() | 請求屬性 |
sessionScope | HttpSession.getAttribute() | 會話屬性 |
applicationScope | ServletContext.getAttribute() | 全域應用屬性 |
param | request.getParameter() | 單值請求參數 |
paramValues | request.getParameterValues() | 多值請求參數 (陣列) |
header | request.getHeader() | 單值 Header |
headerValues | request.getHeaders() | 多值 Header |
cookie | request.getCookies() | Cookie Map (名→值) |
initParam | context.getInitParameter() | 上下文初始化參數 |
pageContext | PageContext | 頁面上下文根物件,可導航 request/response/session/application |
<!-- 表單回顯 -->
<input name="email" value="${param.email}">
<!-- 多選框回顯 -->
<c:forEach items="${paramValues.interests}" var="i">
<input type="checkbox" name="interests" value="${i}" checked>${i}<br>
</c:forEach>
<!-- 判斷登入狀態 -->
<c:if test="${empty sessionScope.user}">
<a href="/login">登入</a>
</c:if>
<!-- 迭代集合 -->
<c:forEach items="${requestScope.products}" var="p" varStatus="st">
${st.index + 1}. ${p.name} - $${p.price}
</c:forEach>
<%= %> 腳本程式碼,配合 <c:out> 自動 HTML 轉義防 XSS。${requestScope.attr} 避免跨作用域同名衝突、提升可讀性。isELIgnored="false"(預設),但確保容器 development=false 避免重複解析開銷。${list.stream().map(x -> x.name).collect(Collectors.toList())}${set1.union(set2)}、${map.filter(k,v -> v > 10)}${bean.property} 直接注入 CDI/Spring Bean(需容器支援)。