EL表达式(JSP)
2023年11月15日大约 5 分钟约 1028 字
介绍
- EL 表达式全称:Expression Language,是表达式语言
- EL 表达式主要是代替 jsp 页面的表达式脚本<%=request.getAttribute("xx")%>
- EL 表达式输出数据的时,比 jsp 的表达式脚本简洁
- EL 表达式基本语法: ${key1}, 你可以理解就是一个语法糖
快速入门
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>el表达式快速入门</title>
</head>
<body>
<h1>el表达式快速入门</h1>
<%
request.setAttribute("name","梁lzw");
%>
<%--
1. 如果name是null,request.getAttribute() 返回的是null字符串
2. 如果name是null,${name},返回的""
--%>
<h1>jsp表达式脚本</h1>
名字 = <%=request.getAttribute("name") == null ? "":request.getAttribute("name")%><br>
<h1>el 表达式</h1>
名字 = ${name}<br/>
</body>
</html>
EL常用输出形式
EL 表达式常用输出 Bean 的普通属性、 数组属性、List 集合属性和 map 集合属性
<%@ page import="com.lzw.entity.Book" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>el表达式输出数据演示</title>
</head>
<body>
<h1>el表达式输出数据演示</h1>
<%
//创建Book对象,放入相关的属性
Book book = new Book();
book.setName("昆虫总动员");
book.setWriter(new String[]{"jack","tom"});
ArrayList<String> reader = new ArrayList<String>();
reader.add("老韩");
reader.add("lzw");
book.setReader(reader);//放入readers
HashMap<String, String> topics = new HashMap<>();
topics.put("topic1","这是我看过最好的动画片");
topics.put("topic2","不错的电影");
book.setTopics(topics);
//把book放入到request域对象
request.setAttribute("bookkey",book);
%>
book对象:${bookkey}<br/>
book.name:${bookkey.name}<br/>
book.writer:${bookkey.writer}<br/>
book.writer[0]:${bookkey.writer[0]}<br/>
book.readers:${bookkey.reader}<br/>
book.readers第二个:${bookkey.reader.get(1)}<br/>
book.readers第二个:${bookkey.reader[1]}<br/>
book.topics:${bookkey.topics}<br/>
book.topics第一个:${bookkey.topics["topic1"]}<br/>
</body>
</html>
EL运算操作符
语法:$
关系运算

逻辑运算

算术运算

EL的empty运算
1.empty 运算可以判断一个数据是否为空,如果为空,返回 true,否则返回 false
2.以下几种情况为空
● 值为 null
● 值为空串的时
● 值是 Object 类型数组,长度为零
● list 集合,元素个数为零
● map 集合,元素个数为零
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>el empty的运算</title>
</head>
<body>
<h1>el empty的运算</h1>
<%
request.setAttribute("k1",null);
request.setAttribute("k2","");
request.setAttribute("k3",new Object[]{});
request.setAttribute("k4",new ArrayList<>());
request.setAttribute("k5",new HashMap<String, Object>());
%>
k1 是否为空= ${empty k1}<br/>
k2 是否为空= ${empty k2}<br/>
k3 是否为空= ${empty k3}<br/>
k4 是否为空= ${empty k4}<br/>
k5 是否为空= ${empty k5}<br/>
k5 是否为空= ${not empty k5}<br/>
</body>
</html>
EL三元运算
表达式 1?表达式 2: 表达式 3
${score >= 60 ? "及格":"不及格"}
EL的11个隐含对象

EL获取四个域数据
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EL 获取四个特定域中的属性</title>
</head>
<body>
<h1>EL 获取四个特定域中的属性</h1>
<%
pageContext.setAttribute("key1", "pageContext_key1 的值");
request.setAttribute("key1", "request_key1 的值");
session.setAttribute("key1", "session_key1 的值");
application.setAttribute("key1", "application_key1 的值");
%>
application 的 key1: ${applicationScope.key1 }<br/>
pageContext 的 key1: ${pageScope.key1 }<br/>
session 的 key1: ${sessionScope.key1 }<br/>
request 的 key1: ${requestScope.key1 }<br/>
</body>
</html>
pageContext对象
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>pageContext 对象的使用</title>
</head>
<body>
<h1>pageContext 对象的使用</h1>
<%--
//通过 request 对象来获取和 HTTP 协议相关的数据
request.getScheme() 它可以获取请求的协议
request.getServerName() 获取请求的服务器 ip 或域名
request.getServerPort() 获取请求的服务器端口号
getContextPath() 获取当前工程路径
request.getMethod() 获取请求的方式(GET 或 POST)
request.getRemoteHost() 获取客户端的 ip 地址
session.getId() 获取会话的唯一标识
--%>
<hr/>
<%--
1.我们可以通过pageContext.request.xx 俩获取和http协议相关的信息
2.相当于替代 request.getMethod()....
--%>
协议: ${ pageContext.request.scheme }<br>
服务器 ip:${ pageContext.request.serverName }<br>
服务器端口:${ pageContext.request.serverPort }<br>
工程路径:${ pageContext.request.contextPath }<br>
请求方法:${ pageContext.request.method }<br>
客户端 ip 地址:${ pageContext.request.remoteHost }<br>
会话 id :${ pageContext.session.id }<br>
<h1>使用 jsp 表达式脚本获取如上信息</h1>
ip 地址: <%=request.getRemoteHost() %> <br>
<h1>使用 el 表达式形式获取信息-简化写法</h1>
<%
pageContext.setAttribute("req", request);
%>
ip 地址: ${req.remoteHost} <br>
获取请求方法: ${req.method} <br>
</body>
</html>