文章目录
EL表达式
EL简介
- EL (Expressibn Language)是JSP内置的表达式语言, EL表达式主要用于代替JSP表达式:<%=%>在页面中做输出操作。
- EL作用:
- EL可以:获取域中的对象、及对象中的属性
- EL也可以:做基本的运算操作
- EL特点:
- EL在得到某个数据时,会自动进行数据类型的转换
- 使用EL表达式输出数据时,如果有则数据输出,如果为null则什么也不输出
EL基本使用
-
基本使用
- ${标识符}:默认从小域到大域检索数据
- ${域对象.标识符}:从指定域中获取数据
-
EL获取域中的对象及对象属性
- ${域对象.对象.属性}
- 属性定义:封装后的成员变量。
- ${域对象.对象.getXxx()}
- ${域对象.对象[“属性”]}
- ${域对象.对象.属性}
<%@ page import="com.example.bean.Student" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<%
String s = "s";
pageContext.setAttribute("s","pageContext");
request.setAttribute("s","request");
session.setAttribute("s","session");
%>
jsp表达式:<%=request.getAttribute("s")%><br>
el:${pageScope.s}
<h2>el获取对象及对象中的属性</h2>
<%
Student student = new Student(1,"zhangSan");
session.setAttribute("stu",student);
%>
jsp:<%=session.getAttribute("stu")%><br>
jsp:<%=((Student)session.getAttribute("stu")).getName()%><br>
el:${sessionScope.stu}<br>
el:${sessionScope.stu.name}<br>
el:${sessionScope.stu.getName()}
</body>
</html>
EL运算符
- 与java一致
- empty运算符:判断空值【true:空】
- “”
- null
- 数组或集合长度为0
- 判断非空
- ${empty 标识符}:判断空值【true:空】
- ${!empty 标识符}:判断非空【true:非空】
- ${not empty 标识符}:判断非空【true:非空】
<h2>empty运算符</h2>
<%
String s1 = "";
String s2 = null;
List<String> list = new ArrayList<String>();//在jsp中,默认支持jdk1.6
session.setAttribute("s1",s1);
session.setAttribute("s2",s2);
session.setAttribute("list",list);
%>
s1是空吗?${empty sessionScope.s1}<br>
s2是空吗?${empty sessionScope.s2}<br>
list是非空吗?${!empty sessionScope.list}<br>
EL11个内置对象【隐含对象|隐式对象】
EL域对象 | 对应Jsp域对象 |
---|---|
pageScope | pageContext |
requestScope | request |
sessionScope | session |
applicationScope | application |
- Jsp域对象,同时通用于EL中
- pageContext:可以直接获取Jsp中,其他8个内置对象
- 其他6个内置对象
- param:获取请求参数,与jsp中request.getParameter()作用一致
- paramValues:获取请求参数【多个】,与request.getParameterValues()作用一致
- header:获取请求头,与jsp中request.getHeader()作用一致。
- headerValues:获取请求头【多个】
- Cookie:获取Cookie信息
- initParam:获取上下文初始化参数,与jsp中ServletContext.getInitParameter()作用一致
- 总结:
- jsp中 pageContext对象 = EL中 pageScope + pageContext
- jsp中request = EL中 requestScope【域】+param【获取请求参数】+header【获取请求头】
- request【获取请求参数+获取请求头+获取URL+转发+域】
- 如何在EL中获取URL信息?
- ${pageContext.request.getContextPath()}或${pageContext.request.contextPath}
<h2>EL11个内置对象</h2>
url:${pageContext.request.getContextPath()}<br>
url:${pageContext.request.contextPath}
JSTL
JSTL简介
- JSTL全程:(JSP Standard Tag Library)【JSP标准标签库】
- JSTL:相当于以标签的形式书写java代码,目的:简化JSP中的脚本片段
- JSTL共有5组标签库,需要掌握3组
- 核心标签库
<%@ taglib prefix = "c" url="http://java.sun.com/jsp/jstl/core"%>
- 格式化标签库
<%@ taglib prefix = "fmt" url="http://java.sun.com/jsp/jstl/fmt"%>
- 函数库
<%@ taglib prefix = "fn" url="http://java.sun.com/jsp/jstl/functions"%>
- 核心标签库
JSTL使用步骤
- 导入两个jar包
- 引入标签库
- 使用
JSTL常用标签
核心标签库
set标签
<h2>set标签-修改域对象的属性</h2>
<%
Student student = new Student(1,"zhangSan");
session.setAttribute("student",student);
%>
修改前name:${sessionScope.student.name}<br>
<c:set target="${sessionScope.student}" property="name" value="liSi"></c:set>
修改后name:${sessionScope.student.name}<br>
流程控制-分支结构
if
<h2>if_demo</h2>
<c:if test="${2>1}" var="rs" scope="request">2>1</c:if>
rs:${requestScope.rs}
choose
<h2>choose[when-otherwise]</h2>
<%--设置age,判断年龄段,
age<18:未成年,
age>=18&&age<30:青年
age>=30&&age<60:中年
age>=60&&age<80:中老年
age>=80:老年
--%>
<c:set var="age" value="18" scope="session"></c:set>
<c:choose>
<c:when test="${sessionScope.age<18}">未成年</c:when>
<c:when test="${sessionScope.age<30}">青年</c:when>
<c:when test="${sessionScope.age<60}">中年</c:when>
<c:when test="${sessionScope.age<80}">中老年</c:when>
<c:otherwise>
老年
</c:otherwise>
</c:choose>
流程控制-循环结构
forEach
<h2>JSTL循环结构【forEach】</h2>
<%--
显示1-100
for(int i = 1;i<100;i+5){sout}
--%>
<c:forEach var="i" begin="1" end="100" step="5">
${i} 
</c:forEach>
forEach迭代集合
<h2>forEach迭代集合</h2>
<%
List<Student> list = new ArrayList<Student>();
list.add(new Student(1,"zhangSan"));
list.add(new Student(2,"liSi"));
list.add(new Student(3,"wangWu"));
request.setAttribute("list",list);
%>
<%--<c:forEach items="${requestScope.list}" var="stu">--%>
<%-- ${stu.id} ${stu.name}<br>--%>
<%--</c:forEach>--%>
<table border="1" width="200px" height = "200px" >
<c:forEach items="${requestScope.list}" var="stu">
<tr>
<td>${stu.id}</td>
<td>${stu.name}</td>
</tr>
</c:forEach>
</table>
重定向
<h2>重定向</h2>
<%--<c:redirect url="index.jsp"></c:redirect>--%>
格式化标签库
- 引入标签库:
<%@taglib prefix="fml" uri="http://java.sun.com/jsp/jstl/fmt" %>
<h2>格式化标签库</h2>
<%
Date date = new Date();
request.setAttribute("date",date);
%>
格式化前:${requestScope.date}<br>
格式化后:<fml:formatDate value="${requestScope.date}" pattern="yyyy-MM-dd hh:mm:ss"></fml:formatDate>
函数库
- 引入标签库:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<h2>函数库</h2>
hello中是否包含HE?<br>
区分大小写:${fn:contains("hello","HE" )}<br>
不区分大小写:${fn:containsIgnoreCase("hello","HE" )}