EL用法

<%@ page import=“cn.laowang.servlets.Address”%>
<%@ page import=“cn.laowang.servlets.User”%>
<%@ page import=“java.util.*”%>
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8” isELIgnored=“false” %>
<%–
EL(Expression Language)表达式
语法:${ }
–%>
<%
int a = 1234;
pageContext.setAttribute(“a”, a);
pageContext.setAttribute(“scope”, “pageContext”);
request.setAttribute(“scope”, “request”);
session.setAttribute(“scope”, “session”);
application.setAttribute(“scope”, “application”);
List list = new ArrayList();
list.add(“aaa”);
list.add(“bbb”);
list.add(“ccc”);
list.add(“ddd”);
request.setAttribute(“list”, list);
Map map = new HashMap();
map.put(“name”, “zhangsan”);
map.put(“gender”, “男”);
map.put(“address”, “天河区”);
request.setAttribute(“map”, map);
User user = new User(“zhangsan”,“kahdfasd”,“张三”,“男”,20,new Address(“广东省”,“广州市”,“天河区”));
request.setAttribute(“user”, user);
request.setAttribute(“aaa”, “”);
request.setAttribute(“bbb”, null);
request.setAttribute(“ccc”, new ArrayList());
request.setAttribute(“ddd”, new HashMap());

%>

Insert title here

EL表达式从哪里取值?一般情况下,从作用域对象中取值

${a}

EL表达式优先从哪个作用域对象中取值?默认情况下从最小作用域取值

${scope}

EL表达式从指定作用域对象中取值:pageScope、requestScope、sessionScope、applicationScope

${pageScope.scope} --- ${requestScope.scope} --- ${sessionScope.scope} --- ${applicationScope.scope}

EL表达式可以解析什么类型的数据?基本数据类型、对象类型、复合对象类型、Map、List

${list[2]} --- ${map.address} --- ${user.realname} --- ${user.addr.town}

EL也可以取请求参数的数据:param、paramValues

${param.name} --- ${paramValues.fav[2]}

EL表达式的[]内也可以做EL表达式的解析

${user.realname} --- ${user["realname"]} --- ${user[param.type]}

EL表达式可以做为空判断:空字符串、null、空map、空list这几个值为true

${empty aaa} --- ${empty bbb} --- ${empty ccc} --- ${empty ddd} --- ${empty scope}

EL表达式可以做基本的逻辑运算,和数学运算

EL表达式没有字符串拼接的概念,也没有字符的概念

${5+4} --- ${5-4} --- ${5*4} --- ${5/4} --- ${5%4} --- ${5+"4"} --- ${5>4} --- ${5 < 4} --- ${5>=4} --- ${5 le 4} --- ${5 eq 4}

EL表达式中pageContext可以获取pageContext作用域对象本身

${pageContext}

你可能感兴趣的:(Java)