一、概念
EL使JSP写起来更简单、简洁。主要用于获取作用域中的数据
二、作用
用于替换作用域对象.getAttribute(“name”);
三、EL的应用(获取基本类型、字符串)
${scope.name}获取具体某个作用域中的数据
${name}获取作用域中的数据,逐级查找(pageContext、request、session、application)
例题
如果
request.setAttribute("key666")
session.setAttribut("key666")
application.setAttribute("key666")
全部都命名为key666,用EL表达式$ {key666}
获取会获取到查询到的第一个(顺序是先查request.setAttribute(“key666”)、session.setAttribute(“key666”)、application.setAttribute(“key666”))
四、为什么要用EL表达式,EL表达式和JSP脚本的区别
假设在页面中key8没有定义
用EL表达式输出这个不存在的key8
<h1>$ {requestScope.key8}<h1>
输出结果是“”,就是什么也没有
但是如果通过作用域对象获取
<h1><%=request.getAttribute("key8")%></h1>
在浏览器会输出一个null
如果是一个表格里面的数据
我们更希望是“ ”,而不是null
所以建议通过EL表达式获取数据
五、EL的应用
5.1 获取引用类型
使用EL获取作用域中的对象调用属性时,只能访问对象的get方法,必须遵守命名规范定义
输出结果为-----
5.2 获取数组、集合的元素
例题
<%@ page import="com.qf.project.entity.Admin" %>
<%@ 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</title>
</head>
<body>
<%
int[] array = {1,3,5,6};
request.setAttribute("arr",array);
List<String> nums = new ArrayList<>();
nums.add("A");
nums.add("B");
nums.add("C");
request.setAttribute("list",nums);
Map<String,String> maps = new HashMap<>();
maps.put("CN","中国");
maps.put("RA","俄罗斯");
maps.put("PN","巴基斯坦");
request.setAttribute("country",maps);
%>
${arr}<br>
${arr[0]}<br/>
${arr[2]}<br/>
<hr>
${list[0]}<br/>
${list[1]}<br/>
${list[2]}<br/>
${list.get(1)}<br/>
<hr>
${country["CN"]}<br/>
${country["RA"]}<br/>
${country["PN"]}<br/>
${country.PN}<br>
</body>
</html>
EL表达式是不支持循环的,所以在访问集合的元素时,是一个一个访问
六、EL表达式运算符
操作符 | 描述 |
---|---|
. | 访问一个Bean属性或者一个映射条目 |
[] | 访问一个数组或者链表的元素 |
+ | 加 |
- | 减 |
* | 乘 |
/ or div | 除 |
% or mod | 取模 |
== or eq | 测试是否相等 |
!= or ne | 测试是否不等 |
< or lt | 测试是否小于 |
> or gt | 测试是否大于 |
<= or le | 测试是否小于等于 |
>= or ge | 测试是否大于等于 |
&& or and | 测试逻辑与 |
|| or or | 测试逻辑非 |
! or not | 测试取反 |
empty | 测试是否空值 |
例题 |
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setAttribute("nums", 420);
%>
<hr/>
<h1>算术运算符</h1>
<h1>${nums + 5}</h1>
<h1>${nums - 5}</h1>
<h1>${nums * 5}</h1>
<h1>${nums div 5}</h1>
<h1>${nums mod 5}</h1>
<h1>关系运算符</h1>
<h1>${nums eq 1234}</h1>
<h1>${nums ne 1234}</h1>
<h1>${nums gt 1234}</h1>
<h1>${nums lt 1234}</h1>
<h1>${nums ge 1234}</h1>
<h1>${nums le 1234}</h1>
<h1>逻辑运算符</h1>
<h1>${nums > 1000 and nums !=200}</h1>
<h1>${nums > 1000 || nums ==2000}</h1>
<h1>${!(nums>1234)}</h1>
<h1>empty运算符</h1>
<h1>${empty nums}</h1>
</body>
</html>
七、获取应用上下文
例题:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EL内置对象</title>
</head>
<body>
<%
String path = request.getContextPath();
%>
<%=path%>
<hr/>
<a href="<%=path%>">Click me</a>
<a href="<%=request.getContextPath()+"/xx/xx/xxx.jsp"%>">Click me</a>
<a href="${pageContext.request.contextPath}/xx/xx/xxx.jsp">Click me</a>
</body>
</html>
在浏览器中查看代码
八、获取Cookie的EL
例题
先定义一个cookie,
@WebServlet(name = "CookieServlet",value = "/cookie")
public class CookieServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie cookie = new Cookie("name1", "libai");
Cookie cookie1 = new Cookie("name2", "xiaobai");
response.addCookie(cookie);
response.addCookie(cookie1);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
8.1不用EL要写的代码很多,如下
在jsp页面调用该cookie
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>获取Cookie的EL</title>
</head>
<body>
<%
Cookie[] cookies = request.getCookies();
String username1 = "";
String username2 = "";
if(cookies!=null){
for (Cookie c:cookies
) {
if(c.getName().equals("name1")){
username1 = c.getValue();
}if(c.getName().equals("name2")){
username2 = c.getValue();
}
}
}
%>
<%=username1%><br/>
<%=username2%>
</body>
</html>
先访问cookie再访问object.jsp,运行结果如图
8.2用EL表达式
只需要两行代码
<h1>${cookie.name1.value}</h1>
<h1>${cookie.name2.value}</h1>
该EL表达式在input标签中可以简化代码
如
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>获取Cookie的EL</title>
</head>
<body>
<%
Cookie[] cookies = request.getCookies();
String username1 = "";
String username2 = "";
if(cookies!=null){
for (Cookie c:cookies
) {
if(c.getName().equals("name1")){
username1 = c.getValue();
}if(c.getName().equals("name2")){
username2 = c.getValue();
}
}
}
%>
<hr/>
<br/>
<input type="text" name="name1" value="<%=username1%>">
</body>
</html>
可以替换成
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>获取Cookie的EL</title>
</head>
<body>
<input type="text" name="name1" value="${cookie.name1.value}">
</body>
</html>