EL表达式
什么是EL表达式,EL表达式的作用?
- EL表达式的全称是:Expression Language。是表达式语言。
- EL表达式的作用:EL表达式主要是代替JSP页面中的表达式脚本在JSP页面中进行数据的输出。
- 原因:EL表达式在输出数据的时候,要比JSP的表达式脚本要简洁很多。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setAttribute("key","值");
%>
表达式脚本输出key的值是:<%=request.getAttribute("key1")==null?"":request.getAttribute("key1")%><br>
EL表达式输出key的值是:${key1}
</body>
</html>
- EL表达式的格式是:${表达式}
- EL表达式在输出null值的时候,输出的是空串。JSP表达式脚本输出null值的时候,输出的是null字符串。
EL表达式搜索域数据的顺序
- EL表达式主要是在JSP页面中输出数据。
- 主要是输出域对象中的数据。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
pageContext.setAttribute("key","pageContext");//1
request.setAttribute("key","request");//2
session.setAttribute("key","session");//3
application.setAttribute("key","application");//4 分别注释刷新页面时,测试过session时需要从新打开页面
%>
${key}
</body>
</html>
- 当四个域中都有相同的key的数据的时候,EL表达式会按照四个域的从小到大的顺序去进行探索,找到输出
EL表达式输出Bean的普通属性,数组属性。List集合属性,map集合属性
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
Person person = new Person();
person.setName("aaaa");
person.setPhones(new String[]{"12312321321","12314144124"});
List<String> cities = new ArrayList<>();
cities.add("北京");
cities.add("上海");
person.setCities(cities);
Map<String,Object> map = new HashMap<>();
map.put("key1","1");
map.put("key2","2");
map.put("key3","3");
person.setMap(map);
pageContext.setAttribute("person",person);
%>
输出Person:${person}<br>
输出name:${person.name}<br>
输出电话:${person.phones[1]}<br>
输出cities集合中的元素值:${person.cities}<br>
输出cities集合中的元素值:${person.cities[1]}<br>
输出map集合中的元素值:${person.map}
输出map集合中的元素值:${person.map.key1}
</body>
</html>
EL的内置对象11个
-
pageScope,requestScope,sessionScope,applicationScope ---- 获取JSP中域中的数据
-
param,paramValues - 接收参数. -----------------------------------------相当于request.getParameter() request.getParameterValues()
-
header,headerValues - 获取请求头信息----------------------------------相当于request.getHeader(name)
-
initParam - 获取全局初始化参数 -------------------------------------------相当于this.getServletContext().getInitParameter(name)
-
cookie - WEB开发中cookie---------------------------------------------------相当于request.getCookies()—cookie.getName()—cookie.getValue()
-
pageContext - WEB开发中的pageContext
-
pageContext可以获得其他八大对象
-
$(pageContext.request.contextPath)--------------------------------相当于<%=pageContext.getRequest().getContextPath()%>这句代码不能实现获得WEB应用的名称
EL获取四个特定域中的属性
- pageScope====pageContext域
- requestScope=====Request域
- sessionScope=====Session域
- applicationScope =====ServletContext域
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
pageContext.setAttribute("key1", "pageContext1");
pageContext.setAttribute("key2", "pageContext2");
request.setAttribute("key2", "request");
session.setAttribute("key2", "session");
application.setAttribute("key2", "application");
%>
${ pageScope.key1 }
${ requestScope.key2 }
${ sessionScope.key2 }
${ applicationScope.key2 }
</body>
</html>