目录
一、JSP域对象
- pageContext 页面容器
- request 请求
- session 服务器存储
- application 公共存储
💕域对象存储的范围大小(从大到小)
application > session > request > pageContext
pageContext: 属性的作用范围仅限当前的jsp页面(范围最小)
request: 属性的作用范围仅限同一个请求(转发有效)
session: 属性的作用范围仅限于一次会话,浏览器打开直到关闭称为一次会话(在此期间会话不失效)
application: 属性的作用范围仅限于当前web应用(范围最大)
二、EL表达式
功能: 替代jsp页面中的复杂代码 (<%= %>)
语法: ${EL expression}
${bean.name}
1.表达式的运用
🌈存入值时,名字不同
先往域对象中存值,我这有两个界面,一是index.jsp,二是test.jsp
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
application.setAttribute("msg1", "hello world");
session.setAttribute("msg2", "你好,世界");
request.setAttribute("msg3", "赛瓦迪卡");
%>
<jsp:forward page="test.jsp"></jsp:forward>
test.jsp
因为pageContext范围仅限当前的jsp页面,所以就将值存在跳转的界面了
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
pageContext.setAttribute("msg4", "玛卡巴卡");
%>
application:${msg1}
<br>
session:${msg2}
<br>
request:${msg3}
<br>
pageContext:${msg4}
</body>
</html>
🌈存入值时,名字相同
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
application.setAttribute("msg", "hello world");
session.setAttribute("msg", "你好,世界");
request.setAttribute("msg", "赛瓦迪卡");
%>
<jsp:forward page="test.jsp"></jsp:forward>
test.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
pageContext.setAttribute("msg", "玛卡巴卡");
%>
application:${msg}
<br>
session:${msg}
<br>
request:${msg}
<br>
pageContext:${msg}
</body>
</html>
可以看到,拿到的全是pageContext所设置的值,当存值名字相同时,取到的范围最小的域对象的值
🌈名字相同,该怎么取值
test.jsp
application:${applicationScope.msg}
<br>
session:${sessionScope.msg}
<br>
request:${requestScope.msg}
<br>
pageContext:${msg}
注意:EL表达式必须是从域对象中取值
2.EL中的隐含对象
EL中的隐含对象的介绍
3.EL运算符
【判断集合中值是否为空 返回true或者false】
application:${applicationScope.msg}
<br>
session:${sessionScope.msg}
<br>
request:${requestScope.msg==null}
<br>
pageContext:${msg}
request是从转发中取值,直接跳转到test界面会取不到值,则是null,所以得到的是true
4.补充(存取对象)
首先得有一个user类
public class USer {
private String name;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public USer() {
// TODO Auto-generated constructor stub
}
public USer(String name, String pwd) {
super();
this.name = name;
this.pwd = pwd;
}
@Override
public String toString() {
return "USer [name=" + name + ", pwd=" + pwd + "]";
}
}
向域对象中存一个对象
<%
USer u=new USer("root","root123");
pageContext.setAttribute("user", u);
%>
取值
${user}
<br>
名字:${user.getName()}
<br>
密码:${user.getPwd()}
名字:${user.name}
<br>
密码:${user.pwd}
以上两种方法皆可以取到对象的属性
将对象存入集合
<%
List<USer> list=new ArrayList<>();
pageContext.setAttribute("us", list);
%>
判断集合是否为空
为空:${us.size()==0},${empty us},不为空:${not empty us}
三、JSTL标签
通用标签:set out remove
条件标签:if
迭代标签:forEach
使用JSTL标签首先需要导入jar包,再导入jstl标签库
<%--tag标签,lib库,导入一个标签库 prefix是标签库的名字--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
1.通用标签
<%-- pageContext.setAttribute("a", 1); --%>
<c:set scope="page" value="1" var="a"></c:set>
<%-- ${a} --%>
<c:out value="${a}"></c:out>
<%-- pageContext.removeAttribute("a"); --%>
<c:remove var="a" scope="page"/>
2.条件标签
模拟登录
<%
//yy这个参数是控制当前用户是否已经登录
String yy=request.getParameter("yy");
pageContext.setAttribute("yy", yy);
%>
<%--如果登录了显示你好--%>
<%--如果没有登录,显示一个登录--%>
<c:if test="${yy!=null}">
<h1>欢迎使用该系统</h1>
<h1>你好!这是首界面</h1>
</c:if>
<c:if test="${yy==null}">
<h1>你现在还没有登录,请点击下面的按钮</h1>
<button>请进行登录</button>
</c:if>
3.迭代标签
模拟商品遍历
<%
List<Goods> list=new ArrayList<>();
for (int i = 0; i < 5; i++) {
Goods g=new Goods();
g.setGoodsId(i);
g.setGoodsName("小谬手机"+i);
g.setGoodsType("搞笑分区"+i);
g.setGoodsPrice(i*100.0);
list.add(g);
}
pageContext.setAttribute("list", list);
%>
<%--商品的遍历--%>
<table border>
<tr>
<th>商品编号</th>
<th>商品名字</th>
<th>商品类型</th>
<th>商品价格</th>
</tr>
<%
for (Goods goods : list) {
goods.getGoodsName();
}
%>
<c:forEach items="${list}" var="goods">
<tr>
<th>${goods.goodsId}</th>
<th>${goods.goodsName}</th>
<th>${goods.goodsType}</th>
<th>${goods.goodsPrice}</th>
</tr>
</c:forEach>
</table>
今天就分享到这啦