EL表达式的使用
-
获取数据
-
设置域对象中的数据
<% pageContext.setAttribute("uname","zhangsan"); // page作用域 request.setAttribute("uname","lisi"); // request作用域 session.setAttribute("uname","wangwu"); // session作用域 application.setAttribute("uname","zaholiu"); // application %>
-
获取域对象的值
<%-- 获取域对象中的数据:默认查找方式为从小到大,找到即止。若四个范围都未找到,则返回空字符串。--%> ${uname} <!-- 输出结果为:zhangsan -->
-
获取指定域对象的值
${pageScope.uname} <!-- page作用域 --> ${requestScope.uname} <!-- request作用域 --> ${sessionScope.uname} <!-- session作用域 --> ${applicationScope.uname} <!-- application作用域 -->
-
获取List
<% List<String> list = new ArrayList<String>(); list.add("aaa"); list.add("bbb"); list.add("ccc"); request.setAttribute("list", list); %>
<%-- 获取List中指定下标的数据 ${list[下标] } 获取集合的长度 ${list.size()} 注: list代表的是存在域对象中的变量名(限域变量名) --%> ${list[1] }
-
获取Map
<% Map map = new HashMap(); map.put("aaa", "111"); map.put("bbb", 2222); map.put("ccc-a", 333); request.setAttribute("map", map); %>
<%-- 获取Map中指定值 ${map["key"] } 或 ${map.key } 注: map代表的是存在域对象中的变量名(限域变量名) --%> ${map.aaa } ${map["bbb"]}
-
获取JavaBean对象
public class User { private Integer userId; private String uname; private String upwd; public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(String upwd) { this.upwd = upwd; } }
<% User user = new User(); user.setUserId(1); user.setUname("zhangsan"); user.setUpwd("123456"); request.setAttribute("user",user); %>
<%-- JavBean中的属性字段需要提供get方法 --%> ${user} <%-- 获取对象 --%> ${user.uname} <%--获取对象中的属性--%>
-
empty
<%-- empty 判断域对象是否为空。为空,返回true;不为空返回false; ${empty 限域变量名 } 判断对象是否不为空。 ${!empty 限域变量名 } --%> ${empty uname} ${empty list} ${empty map} ${empty user}
-
EL运算
<% request.setAttribute("a", 10); request.setAttribute("b", 2); request.setAttribute("c", "aa"); request.setAttribute("d", "bb"); %>
-
等值判断
<%-- 比较两个值是否相等,返回true或false == 或 eq --%> ${a == b } ${c == d } ${c eq d } ${a == 5 } ${c == 'aa' }
-
算术运算
<%-- 加法: + 减法: - 乘法: * 除法: / 或 div --%> ${a + b } ${a / b } 或 ${a div b }
-
大小比较
%-- 大于:> 小于:< 大于等于:>= 小于等于:<= --%> ${a > b} ${a + 1 > 10 } ${a + b >= 10 } ${a > b && b > 5 } ${a + b > 10 || a - b > 5 }
-