JSP 表达式语言
EL 的语法、隐式对象、运算符、空值处理与函数调用。
基本语法
${user.name}
${user.address.city}
${users[0].name}
${map['key']}
${list.size()}
${1 + 2 * 3}
${empty user}${user.name} calls getUser().getName(); a miss yields null printed as empty. . and [] mix freely, and [] accepts a variable: ${users[i].name}.
隐式对象
| Object | Returns |
|---|---|
pageScope | page 作用域里的属性 Map |
requestScope | request 作用域属性 |
sessionScope | session 作用域属性 |
applicationScope | application 作用域属性 |
param | 请求参数(单值) |
paramValues | 请求参数数组 |
header / headerValues | 请求头 |
cookie | Cookie Map |
pageContext | PageContext 本身 |
initParam | context-param |
运算符
${a + b} ${a - b} ${a * b} ${a / b} ${a % b}
${a == b} ${a != b} ${a < b} ${a >= b}
${a and b} ${a or b} ${not a}
${empty list} ${not empty list}
${a > 0 ? '正' : '非正'}
${user.name == 'Ada' ? 'is Ada' : 'not Ada'}== compares values (string content, not identity), so ${name == 'Ada'} works. That differs from Java's == on String and is deliberate in EL.
空值处理
<%-- 以下四种情况 empty 都为 true --%>
${empty nullValue} <%-- null --%>
${empty ""} <%-- 空串 --%>
${empty emptyList} <%-- 空集合 --%>
${empty emptyArray} <%-- 空数组 --%>
<%-- 直接输出会显示空串,不会显示 null --%>
<p>${missing.name}</p>
<%-- 取默认值 --%>
${empty user.nickname ? user.name : user.nickname}${missing.name} does not throw even though missing is null — EL short-circuits to empty. That keeps the page from 500ing on missing data, but also hides "forgot to set the attribute" bugs.
调用方法
${list.size()}
${map.containsKey('k')}
${str.toUpperCase()}
${user.getDisplayName()}
<%-- 需要 JSP 2.2+ / EL 2.2,且方法必须是 public --%>if, for or assignment. Use <c:if> / <c:forEach> for control flow; nesting ternaries three deep inside ${} reads far worse than the JSTL equivalent.关闭 EL
<%-- 整页关闭:页面上的 ${} 原样输出,适合演示模板语法 --%>
<%@ page isELIgnored="true" %>
<%-- 局部转义:输出字面量 ${} --%>
\${notEvaluated}isELIgnored="true" is useful on pages that teach EL syntax, or when upgrading an old project where legacy ${} must not be evaluated.
基本语法
${user.name}
${user.address.city}
${users[0].name}
${map['key']}
${list.size()}
${1 + 2 * 3}
${empty user}${user.name} 等价于调用 getUser().getName(),找不到时返回 null 并输出空串。. 和 [] 可以混用,[] 里能放变量:${users[i].name}。
隐式对象
| 对象 | 返回 |
|---|---|
| page 作用域 | page 作用域里的属性 Map |
| request 作用域 | request 作用域属性 |
| session 作用域 | session 作用域属性 |
| application 作用域 | application 作用域属性 |
| 参数 | 请求参数(单值) |
| 多值参数 | 请求参数数组 |
| 请求头 | 请求头 |
| Cookie | Cookie Map |
| 页面上下文 | PageContext 本身 |
| 初始化参数 | context-param |
运算符
${a + b} ${a - b} ${a * b} ${a / b} ${a % b}
${a == b} ${a != b} ${a < b} ${a >= b}
${a and b} ${a or b} ${not a}
${empty list} ${not empty list}
${a > 0 ? '正' : '非正'}
${user.name == 'Ada' ? 'is Ada' : 'not Ada'}== 做的是值比较(对字符串比内容,不是比引用),所以 ${name == 'Ada'} 能正常工作。这和 Java 里 String 用 == 比引用完全不同,是 EL 特意做的处理。
空值处理
<%-- 以下四种情况 empty 都为 true --%>
${empty nullValue} <%-- null --%>
${empty ""} <%-- 空串 --%>
${empty emptyList} <%-- 空集合 --%>
${empty emptyArray} <%-- 空数组 --%>
<%-- 直接输出会显示空串,不会显示 null --%>
<p>${missing.name}</p>
<%-- 取默认值 --%>
${empty user.nickname ? user.name : user.nickname}${missing.name} 里 missing 是 null,但 EL 不会抛 NPE——它直接短路返回空。这让页面在数据缺失时也不会 500,但也容易掩盖「忘了放数据」的问题。
调用方法
${list.size()}
${map.containsKey('k')}
${str.toUpperCase()}
${user.getDisplayName()}
<%-- 需要 JSP 2.2+ / EL 2.2,且方法必须是 public --%>if、for、赋值语句。控制流程要靠 <c:if> / <c:forEach>;在 ${} 里堆三元表达式嵌套三层,可读性会比 JSTL 差很多。关闭 EL
<%-- 整页关闭:页面上的 ${} 原样输出,适合演示模板语法 --%>
<%@ page isELIgnored="true" %>
<%-- 局部转义:输出字面量 ${} --%>
\${notEvaluated}isELIgnored="true" 在写「教别人 EL 语法」的页面时有用;也可以用在老项目升级时,避免旧页面里的 ${} 被意外求值。