EL表达式(熟悉)
- EL(Expression Language)表达式提供了在JSP中简化表达式的方法,可以方便地访问各种数据并输出。
主要功能
- 依次访问pageContext、request、session和application作用域对象存储的数据。
- 获取请求参数值。
- 访问Bean对象的属性。
- 访问集合中的数据。
- 输出简单的运算结果。
访问内置对象的数据
访问方式
- <%=request.getAttribute(“ varName”)%>
- 用EL实现: ${ varName }
执行流程
![在这里插入图片描述](https://img-blog.csdnimg.cn/7dd5bc7dd4cf4ddfb705441966a3abaf.png)
访问请求参数的数据
- 在EL之前使用下列方式访问请求参数的数据
request.getParameter(name);
request.getParameterValues(name); - 在EL中使用下列方式访问请求参数的数据
param:接收的参数只有一个值。
paramValues:接受的参数有多个值。
<!-- 获取指定参数的数值 -->
${param.name}
<!-- 获取指定参数中指定下标的数值 -->
${paramValues.hobby[0]}
访问Bean对象的属性
- 方式一: $ { 对象名 . 属性名 },例如:${user.name}
- 方式二: $ { 对象名 [“属性名”] },例如:${user[“name”]}
- 当要存取的属性名中包含一些特殊字符,如: . 或 ,等并非字母或数字的符号,就一定要使用 [ ]而不是.的方式
<%
request.setAttribute(“prop”,“age”);
%>
<!-- 相当于表达式中写一个变量 -->
${ user[prop] }
访问集合中的数据
<!-- student为ArrayList类型的对象 -->
${students[0].name}
常用的内置对象
![在这里插入图片描述](https://img-blog.csdnimg.cn/2bf2aeafb627495786e693eb28e8602f.png)
常用的运算符
常用的算术运算符
![在这里插入图片描述](https://img-blog.csdnimg.cn/0c9c12b1878a472f956ce6673b3f6af7.png)
常用的关系运算符
![在这里插入图片描述](https://img-blog.csdnimg.cn/652f4a813ade49e1bb231d6801c588e8.png)
常用的逻辑运算符
![在这里插入图片描述](https://img-blog.csdnimg.cn/d963c6439bba4a7484f5d272b91c5c44.png)
条件运算符
${条件表达式? 语句1 : 语句2}
验证运算符
${empty 表达式}
返回布尔值判断表达式是否为"空"值,null值、无元素的集合或数组、长度为零的String被认为是空值。
JSTL标签(熟悉)
<!-- prefix属性用于指定库前缀 -->
<!-- uri属性用于指定库的标识 -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
常用核心标签
输出标签
<c:out></c:out> 用来将指定内容输出的标签
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>实现out输出标签的使用</title>
</head>
<body>
<c:out value="Hello World"></c:out>
</body>
</html>
设置标签
<c:set></c:set> 用来设置属性范围值的标签
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>实现set标签的使用</title>
</head>
<body>
<%-- 表示设置一个名字为name的属性,对应的数值为zhangfei,有效范围为:page --%>
<%-- pageContext.setAttibute("name", "zhangfei") --%>
<c:set var="name" value="zhangfei" scope="page"></c:set>
<%-- 使用out标签打印出来 --%>
<c:out value="${name}"></c:out>
<hr/>
<%-- 设置一个对象的属性值并打印出来 --%>
<jsp:useBean id="person" class="com.lagou.demo01.Person" scope="page"></jsp:useBean>
<c:set property="name" value="guanyu" target="${person}"></c:set>
<c:set property="age" value="35" target="${person}"></c:set>
<c:out value="${person.name}"></c:out>
<c:out value="${person.age}"></c:out>
</body>
</html>
删除标签
<c:remove></c:remove> 用来删除指定数据的标签
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>实现remove标签的使用</title>
</head>
<body>
<%-- 设置一个属性值并打印 --%>
<c:set var="name" value="liubei" scope="page"></c:set>
<c:out value="${name}"></c:out>
<hr/>
<%-- 删除这个属性值后再次打印 --%>
<c:remove var="name" scope="page"></c:remove>
<c:out value="${name}" default="无名"></c:out>
</body>
</html>
单条件判断标签
<c:if test =“EL条件表达式”>
满足条件执行
</c:if >
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>实现if标签的使用</title>
</head>
<body>
<%-- 设置一个变量以及对应的数值 --%>
<c:set var="age" value="17" scope="page"></c:set>
<c:out value="${age}"></c:out>
<hr/>
<%-- 判断该年龄是否成年,若成年则提示已经成年了 --%>
<c:if test="${age >= 18}">
<c:out value="已经成年了!"></c:out>
</c:if>
</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" %>
<%--
Created by IntelliJ IDEA.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>实现choose标签的使用</title>
</head>
<body>
<%-- 设置一个变量代表考试的成绩并指定数值 --%>
<c:set var="score" value="59" scope="page"></c:set>
<c:out value="${score}"></c:out>
<hr/>
<%-- 进行多条件判断和处理 --%>
<c:choose>
<c:when test="${score > 60}">
<c:out value="成绩不错,继续加油哦!"></c:out>
</c:when>
<c:when test="${score == 60}">
<c:out value="60分万岁,多一份浪费!"></c:out>
</c:when>
<c:otherwise>
<c:out value="革命尚未成功,同志仍需努力!"></c:out>
</c:otherwise>
</c:choose>
</body>
</html>
循环标签
<c:forEach var=“循环变量” items=“集合”>
…
</c:forEach>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>实现循环标签的使用</title>
</head>
<body>
<%
String[] sArr = {"11", "22", "33", "44", "55"};
pageContext.setAttribute("sArr", sArr);
%>
<%-- 使用循环标签遍历数组中的所有元素 --%>
<c:forEach var="ts" items="${sArr}">
<c:out value="${ts}"></c:out>
</c:forEach>
<hr/>
<%-- 跳跃性遍历 间隔为2 也就是跳过一个遍历一个 --%>
<c:forEach var="ts" items="${sArr}" step="2">
<c:out value="${ts}"></c:out>
</c:forEach>
<hr/>
<%-- 指定起始和结尾位置 从下标1开始到3结束,包含1和3--%>
<c:forEach var="ts" items="${sArr}" begin="1" end="3">
<c:out value="${ts}"></c:out>
</c:forEach>
</body>
</html>
常用函数标签
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%--
Created by IntelliJ IDEA.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>实现常用函数标签的使用</title>
</head>
<body>
<%
pageContext.setAttribute("var", "Hello World!");
%>
原始字符串为:${var}<br/> <%-- HelloWorld --%>
判断该字符串是否包含指定字符串的结果为:${fn:contains(var, "Hello")}<br/> <%-- true --%>
将字符串中所有字符转换为大写的结果为:${fn:toUpperCase(var)}<br/> <%-- HELLO WORLD!--%>
将字符串中所有字符转换为小写的结果为:${fn:toLowerCase(var)}<br/> <%-- hello world!--%>
</body>
</html>
常用格式化标签
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page import="java.util.Date" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%--
Created by IntelliJ IDEA.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>实现格式标签的使用</title>
</head>
<body>
<%
Date date = new Date();
pageContext.setAttribute("date", date);
%>
当前系统时间为:${date}
<hr/>
<fmt:formatDate value="${date}" pattern="yyyy-MM-dd HH:mm:ss"></fmt:formatDate>
</body>
</html>
自定义标签
- 编写标签类继承SimpleTagSupport类或TagSupport类并重写doTag方法或doStartTag方法。
public class HelloTag extends SimpleTagSupport {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void doTag() throws JspException, IOException {
JspWriter out = this.getJspContext().getOut();
out.write("自定义标签哦!" + name);
out.close();
}
}
- 定义标签库文件(tld标签库文件)并配置标签说明文件到到WEB-INF下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<tlib-version>1.0</tlib-version>
<short-name>my</short-name>
<uri>http://lagou.com</uri>
<tag>
<name>hello</name>
<tag-class>com.lagou.demo01.HelloTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>name</name>
<required>true</required>
</attribute>
</tag>
</taglib>
<%@ taglib prefix="hello" uri="http://lagou.com" %>