EL表达式
1.1 EL表达式
1.1.1 基本定义
EL(Expression Language)表达式提供了在JSP中简化表达式的方法,可以方便地访问各种数据并输出。
| 区别 | JSP表达式 | EL表达式 |
|---|---|---|
| 语法 | <%=变量名或表达式%> | $ |
| 输出哪里的值 | 是脚本变量值 | 作用域中值,如果要使用EL取出变量的值 必须先将变量保存在作用域中。 |
1.1.2 主要功能
- 依次访问pageContext、request、session和application作用域对象存储的数据。
- 获取请求参数值。访问Bean对象的属性。
- 访问集合中的数据。输出简单的运算结果。
1.1.3 内置对象
访问方式
<%=request.getAttribute(“ varName”)%>
用EL表达式实现: ${ varName }
代码示例
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>内置对象中获取数据</title>
</head>
<body>
<%
pageContext.setAttribute("username1", "pageContext对象中的属性值:Kobe");
request.setAttribute("username2", "request对象中的属性值:Curry");
session.setAttribute("username3", "session对象中的属性值:James");
application.setAttribute("username4", "session对象中的属性值:Harden");
%>
<%--使用JSP中原始方式获取数据--%>
<%--
<%= "nam1的数值为:" + pageContext.getAttribute("username1")%><br/>
<%= "nam2的数值为:" + request.getAttribute("username2")%><br/>
<%= "nam3的数值为:" + session.getAttribute("username3")%><br/>
<%= "nam4的数值为:" + application.getAttribute("username4")%><br/>
--%>
<%--使用EL表达式实现获取数据--%>
<h3>username1的数值为:${username1}</h3>
<h3>username2的数值为:${username2}</h3>
<h3>username3的数值为:${username3}</h3>
<h3>username4的数值为:${username4}</h3>
</body>
</html>
执行结果

8.1.4 请求参数数据
- 在EL之前使用下列方式访问请求参数的数据
request.getParameter(name);
request.getParameterValues(name);
- 在EL中使用下列方式访问请求参数的数据
param:接收的参数只有一个值。
paramValues:接受的参数有多个值.
代码示例
传递参数
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>传递参数</title>
</head>
<body>
<form action="el_param02.jsp" method="post">
用户名: <input type="text" name="name"/><br/>
爱好:<input type="checkbox" name="hobby" value="Java"/>Java<br/>
<input type="checkbox" name="hobby" value="Vue.js"/>Vue.js<br/>
<input type="checkbox" name="hobby" value="Python"/>Python<br/>
<input type="submit" value="提交"/><br/>
</form>
</body>
</html>
请求参数数值的获取
<%@ page import="java.util.Arrays" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>请求参数数值的获取</title>
</head>
<body>
<%--设置编码--%>
<%
request.setCharacterEncoding("utf-8");
%>
<%--原始方式获取请求参数值 --%>
<%--
<%="用户名:" + request.getParameter("name")%><br/>
<%="爱好:" + Arrays.toString(request.getParameterValues("hobby"))%><br/>
--%>
<%--2.使用EL表达式获取参数值--%>
用户名: ${param.name}<br/>
爱好: ${paramValues.hobby[0]}<br/>
</body>
</html>
执行结果

1.1.5 对象属性获取
代码示例
<%@ page import="cn.guardwhy.pojo.Person" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Bean对象中属性的获取</title>
</head>
<body>
<%
Person person = new Person();
person.setName("Curry");
person.setAge(32);
pageContext.setAttribute("person", person);
pageContext.setAttribute("let1", "name");
pageContext.setAttribute("let2", "age");
%>
<%--1.原始方式输出属性--%>
<%= "原始方式:"%><br/>
<%="用户名:" + person.getName()%><br/>
<%="年龄:" + person.getAge()%><br/><br/>
<%--2.EL表达式输出--%>
<%= "EL表达式输出"%><br/>
<%--
用户名: ${person.name}<br/>
年龄: ${person.age}<br/>
--%>
用户名:${person["name"]}<br/>
年龄: ${person["age"]}<br/><br/>
<%-- 3.动态取值 --%>
<%= "动态取值"%><br/>
用户名:${person[let1]}
</body>
</html>
执行结果

1.1.6 获取集合元素
代码示例
<%@ 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>
<%
// 1.创建集合添加元素
List<String> list = new ArrayList<>();
list.add("curry");
list.add("kobe");
list.add("james");
// 2.将整个集合放入到指定内置对象中
pageContext.setAttribute("list", list);
%>
<%-- EL表达式获取集合元素 --%>
下标为0元素:${list[0]}<br/>
下标为1元素:${list[1]}<br/>
下标为2元素:${list[2]}<br/>
<hr/>
<%
// 1.创建map集合添加元素
Map<String, Integer> map = new HashMap<>();
map.put("kobe", 1);
map.put("curry", 2);
map.put("LeBron.james", 3);
// 2.将整个集合放入到指定内置对象中
pageContext.setAttribute("map", map);
%>
<%-- EL表达式获取Map集合元素--%>
Map集合元素:${map}<br/>
特殊字符Value: ${map["LeBron.james"]}<br/>
</body>
</html>
执行结果

1.1.7 常用的内置对象
| 类别 | 标识符 | 基本描述 |
|---|---|---|
| JSP | pageContext | PageContext 处理当前页面 |
| 页面域 | pageScope | 同页面作用域属性名称和值有关的Map类 |
| 请求域 | requestScope | 同请求作用域属性的名称和值有关的Map类 |
| 会话域 | sessionScope | 同会话作用域属性的名称和值有关的Map类 |
| 上下文域 | applicationScope | 同应用程序作用域属性的名称和值有关的Map类 |
| 请求参数 | param | 根据名称存储请求参数的值的Map类 |
| paramValues | 把请求参数的所有值作为一个String数组来存储的Map类 |
1.2 JSTL标签
8.2.1 基本概念
- JSTL( JSP Standard Tag Library ) 被称为JSP标准标签库。
- 可以利用这些标签取代JSP页面上的Java代码,从而提高程序的可读性,降低程序的维护难度。
8.2.2 导入相关依赖
<!-- JSTL表达式的依赖 -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!-- standard标签库 -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
8.2.3 常用核心标签
输出标签
<c:out></c:out> 用来将指定内容输出的标签
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>输出标签</title>
</head>
<body>
<!--输出标签-->
<c:out value="hello jsp"></c:out>
</body>
</html>
设置标签
<c:set></c:set> 用来设置属性范围值的标签
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>set标签设置</title>
</head>
<body>
<%--设置属性--%>
<c:set var="username" value="Curry" scope="page"></c:set>
<%--使用out标签打印--%>
<c:out value="username:${username}"></c:out><br/>
<%--设置对象属性值并且打印--%>
<jsp:useBean id="person" class="cn.guardwhy.pojo.Person" scope="page"></jsp:useBean>
<c:set property="name" value="guardwhy" target="${person}"></c:set>
<c:set property="age" value="26" target="${person}"></c:set>
<c:out value="username:${person.name}"></c:out><br/>
<c:out value="age:${person.age}"></c:out>
</body>
</html>
单条件判断标签
<c:if test =“EL条件表达式”>
满足条件执行
</c:if
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>if标签</title>
</head>
<body>
<%--设置变量--%>
<c:set var="age" value="21" scope="page"></c:set>
<c:out value="年龄是:${age}"></c:out><br/>
<%--判断年龄是否成年--%>
<c:if test="${age >=18}">
<%--输出结果--%>
<c:out value="恭喜你,正式长大了"></c:out>
</c:if>
</body>
</html>
remove标签
<c:remove></c:remove> 用来删除指定数据的标签
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>remove标签</title>
</head>
<body>
<%--设置属性--%>
<c:set var="username" value="guardwhy" scope="page"></c:set>
<c:out value="username:${username}"></c:out>
<hr/>
<%--删除属性--%>
<c:remove var="username" scope="page"></c:remove>
<c:out value="username:${username}" default="NULL"></c:out>
</body>
</html>
多条件判断标签
<c:choose >
<c:when test =“EL表达式”>
满足条件执行
</c:when>
…
<c:otherwise>
不满足上述when条件时执行
</c:otherwise>
</c:choose
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>实现choose标签</title>
</head>
<body>
<%-- 设置变量并指定数值 --%>
<c:set var="age" value="17" scope="page"></c:set>
<c:out value="age:${age}"></c:out>
<hr/>
<%--进行多条件判断--%>
<c:choose>
<c:when test="${age > 18}">
<c:out value="恭喜你,已经成年了。可以好好的happy了"></c:out>
</c:when>
<c:when test="${age == 18}">
<c:out value="才刚刚18岁,好好学习"></c:out>
</c:when>
<c:otherwise>
<c:out value="未成年,晚上早点休息"></c:out>
</c:otherwise>
</c:choose>
</body>
</html>
常用函数标签
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>常用函数标签</title>
</head>
<body>
<%
pageContext.setAttribute("let", "Hello JSP");
%>
原生字符串: ${let}<br/>
判断字符串是否包含指定字符串:${fn:contains(let,"Hello")}<br/>
将原生字符串转换为大写:${fn:toUpperCase(let)}<br/>
将原生字符串转换为小写:${fn:toLowerCase(let)}<br/>
</body>
</html>
循环标签
<c:forEach var=“循环变量” items=“集合”>
…
</c:forEach>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>实现循环标签</title>
</head>
<body>
<%
// 1.定义数组
String[] array = {"12", "21", "31", "28", "56"};
// 2.设置值
pageContext.setAttribute("array", array);
%>
<%-- 循环遍历数组中的所有元素 --%>
<c:out value="排序后:"></c:out>
<c:forEach var="arr" items="${array}">
<c:out value="${arr}"></c:out>
</c:forEach>
<hr/>
<%-- 跳跃性遍历间隔为2 --%>
<c:out value="排序后(跳跃遍历):"></c:out>
<c:forEach var="arr" items="${array}" step="2">
<c:out value="${arr}"></c:out>
</c:forEach>
<hr/>
<%-- 指定起始和结尾位置 从下标1开始到3结束 --%>
<c:out value="排序后(指定位置):"></c:out>
<c:forEach var="arr" items="${array}" begin="1" end="3">
<c:out value="${arr}"></c:out>
</c:forEach>
</body>
</html>
执行结果


浙公网安备 33010602011771号