核心對照表
| Java/JS 符號 | EL 關鍵字 | 含義 |
== | eq | 等於 |
!= | ne | 不等於 |
> | gt | 大於 |
< | lt | 小於 |
>= | ge | 大於等於 |
<= | le | 小於等於 |
實戰示例
<!-- JSP / Thymeleaf / Facelets 通用 -->
<c:if test="${user.age ge 18}">成年</c:if>
<c:if test="${status eq 'ACTIVE'}">啟用中</c:if>
<c:choose>
<c:when test="${score gt 90}">優秀</c:when>
<c:when test="${score ge 60}">及格</c:when>
<c:otherwise>不及格</c:otherwise>
</c:choose>
避坑提醒
- 不支持
&& / ||:邏輯與用 and,邏輯或用 or,非用 not 或 !。
- 字符串比較:
${str1 eq str2} 等同於 str1.equals(str2),不會拋 NPE(任一為 null 返回 false)。
- 數字比較:自動拆箱,null 視為 0,
${null gt 5} 為 false。
- 優先級:關系運算符 > 邏輯運算符 > 三元運算符
? :。
完整運算符優先級(高到低)
[] . (屬性訪問)
() (分組)
- (一元) not ! empty
* / div % mod
+ - (二元)
< > <= >= lt gt le ge
== != eq ne
and
or
? : (三元)