语法:${ 运算表达式 } , EL 表达式支持如下运算符:
<body>
<h2>关系运算</h2>
${10 == 10} 或 ${10 eq 12} <br/>
${10 ne 12} 或 ${10 != 10} <br/>
${10 < 5} 或 ${5 lt 10} <br/>
${10 > 5} 或 ${5 gt 10} <br/>
${10 <= 5} 或 ${5 le 10} <br/>
${10 >= 5} 或 ${5 ge 10} <br>
<h2>逻辑运算</h2>
${12 == 12 && 12 < 10} 或 ${12 == 12 and 12 < 10}<br>
${12 == 12 || 12 < 10} 或 ${12 == 12 or 12 < 10}<br>
${!true} 或 ${not true}<br>
<h2>算术运算</h2>
${12 + 18}<br>
${18 - 10}<br>
${5 * 5}<br>
${12 / 6} 或 ${12 div 6}<br>
${10 % 2} 或 ${10 mod 5}<br>
</body>
empty 运算
empty 运算可以判断一个数据是否为空,如果为空,则输出 true,不为空输出 false。
以下几种情况为空:
1、值为 null 值的时候,为空
2、值为空串的时候,为空
3、值是 Object 类型数组,长度为零的时候
4、list 集合,元素个数为零
5、map 集合,元素个数为零
<body>
<h2>empty运算</h2>
<%
request.setAttribute("emptyNull",null);
request.setAttribute("emptyStr","");
request.setAttribute("emptyArr",new Object[]{});
List<String> list = new ArrayList<>();
request.setAttribute("emptyList",list);
Map<String,Object> map = new HashMap<>();
request.setAttribute("emptyMap",map);
%>
${!empty emptyNull} <br/>
${empty emptyStr} <br/>
${empty emptyArr} <br/>
${empty emptyList} <br/>
${empty emptyMap} <br/>
</body>
三元运算符
语法格式:表达式 1?表达式 2:表达式 3
如果表达式 1 的值为真,返回表达式 2 的值,如果表达式 1 的值为假,返回表达式 3 的值。
<body>
<h2>三元运算符</h2>
${10 % 2 == 0 ? "偶数" : "奇数"}
</body>
“.”点运算 和 [] 中括号运算符
.点运算,可以输出对象中某个属性的值。
[]中括号运算,可以输出有序集合中某个元素的值。
并且[]中括号运算,还可以输出 map 集合中 key 里含有特殊字符的 key 的值。
<body>
<h2>.与[]运算</h2>
<%
Map<String,Object> map1 = new HashMap<>();
map1.put("a.a.a","aaaVal");
map1.put("b+b+b","bbbVal");
request.setAttribute("map",map1);
%>
${map['a.a.a']} <br/>
${map['b+b+b']} <br/>
</body>
EL 表达式: 11 个隐含对象
EL 个表达式中 11 个隐含对象,是 EL 表达式中自己定义的,可以直接使用。
<body>
<h2>获取域数据</h2>
<%
pageContext.setAttribute("key1","pageContext1");
pageContext.setAttribute("key2","pageContext2");
request.setAttribute("key1","request");
session.setAttribute("key1","session");
application.setAttribute("key1","application");
%>
${pageScope.key2} <br/>
${pageContext.request.getAttribute("key1")} <br/>
${sessionScope.key1} <br/>
${applicationScope.key1} <br/>
</body>