EL表达式 (Expression Language)
1、概念
- EL使JSP写起来更简单、简洁。主要用于
获取作用域中的数据
2、作用
- 用于
替换作用域对象.getAttribute("name")
;
EL的应用
1.获取字符串:
- ${[scope.name}获取具体某个作用域中的数据
- $Iname}获取作用域中的数据,逐级查找(pageContext、request、session、application)
示例1:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EL表达式</title>
</head>
<body>
<%
request.setAttribute("key1","value1");
session.setAttribute("key2","value2");
application.setAttribute("key3","value3");
%>
<h1>通过作用域随想获取数据:</h1>
<%-- 通过作用域对象获取数据--%>
<h1><%=request.getAttribute("key1")%></h1>
<h1><%=session.getAttribute("key2")%></h1>
<h1><%=application.getAttribute("key3")%></h1>
<hr/>
<%-- 通过EL表达式获取数据--%>
<h1>通过EL表达式获取数据:</h1>
<h1>${requestScope.key1}</h1>
<h1>${sessionScope.key2}</h1>
<h1>${applicationScope.key3}</h1>
<%-- EL更加简化--%>
<hr/>
${key1}
${key2}
${key3}
<hr/>
<%-- EL与JSP的区别--%>
<%=pageContext.findAttribute("key8")%>
${key8}
</body>
</html>
示例1:
2.获取引用对象:
示例2:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.qf.entity.User" %>
<html>
<head>
<title>EL获取对象</title>
</head>
<body>
<%
User user=new User("gavin","123456");
request.setAttribute("user",user);
%>
<%-- jsp获取引用对象--%>
<%
User u=(User)request.getAttribute("user");
out.println(u.getUsername());
out.println(u.getPassword());
%>
<hr/>
<%-- 通过EL获取引用对象--%>
${requestScope.user}<br/>
${requestScope.user.username}<br/>
${user.password}
</body>
</html>
示例2:
3.获取数组、集合:
示例3:
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>数组、集合访问</title>
</head>
<body>
<%-- 编写数据--%>
<%
int[] array=new int[]{1,2,3};
request.setAttribute("array",array);
List<String> list=new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
request.setAttribute("list",list);
Map<String,String> map=new HashMap<>();
map.put("US","美国");
map.put("Ch","中国");
request.setAttribute("map",map);
%>
<%-- 通过el表达式获取数组--%>
${array[0]}<br/>
${array[1]}<br/>
${array[2]}<br/>
<hr/>
<%-- 获取集合list--%>
${list.get(0)}<br/>
${list.get(0)}<br/>
${list.get(0)}<br/>
<hr/>
<%-- 获取集合map--%>
${map["US"]}<br/>
${map["Ch"]}<br/>
${map.US}
</body>
</html>
示例3:
EL的运算符
示例:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EL表达式操作符</title>
</head>
<body>
<%
request.setAttribute("nums",1234);
request.setAttribute("str1","");
request.setAttribute("str2",null);
%>
<h1>算数运算符</h1>
${nums + 5}<br/>
${nums - 5}<br/>
${nums * 5}<br/>
${nums / 5}<br/>
${nums % 5}<br/>
<hr/>
<h1>关系运算符</h1>
${nums == 1234}<br/>
${nums <= 1234}<br/>
${nums >= 1234}<br/>
${nums != 1234}<br/>
${nums < 1234}<br/>
${nums > 1234}<br/>
<hr/>
<h1>逻辑运算符</h1>
${nums > 1000 && nums != 1200}<br/>
${nums > 1000 || nums == 2000}<br/>
${!(nums > 1000)}
<hr/>
<h1>empty测试</h1>
${empty str1}<br/>
${empty str2}
</body>
</html>
示例:
EL的运算符
1、pageContext获取上下文
2、获取cookie对象
示例:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EL内置对象</title>
</head>
<body>
<%
String path=request.getContextPath();
%>
<%=path%>
<br/>
<%-- 获取上下文方式一:--%>
<a href="<%=request.getContext()%>/manager/safe/xxxController">Click me</a><br/>
<%-- 获取上下文方式二(常用):--%>
<a href="${pageContext.request.contextPath}/manager/safe/xxxController">Click target</a>
</body>
</html>
示例: