目录
EL表达式
EL表达式简介
EL表达式即:Expressionla Language
表达式语言的灵感来自于 ECMAScript和XPath 表达式语言
主要功能
简化JSP中的java代码
EL表达式格式
所有EL表达式的格式都是以"${变量}"表示
EL表达式变量属性范围
从大向小:application→ session→ request→ pageContext
当四个域名字一样默认取pageContext
举例:
index.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>
<%
session.setAttribute("mgs","hello word");//给属性名为mgs1设置
application.setAttribute("mgs1","hello word2");
%>
</body>
</html>
test.jsp界面
<%@page import="com.pojo.zking.User"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>session:${mgs}</h1>
<h1> application:${mgs1}</h1>
</body>
</html>
JSTL
JSTL简介
JSTL(JavaServerPages Standard Tag Library)JSP标准标签库
JSTL功能
实现JSP页面中逻辑处理,可编写各种动态jsp页面
JSTL通常会与EL表达式合作实现JSP页面的编码
JSTL使用
导入jar包
引入JSTL语句
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
利用JSTL给域赋值&进行打印&移除域属性
<%@page import="com.pojo.zking.User"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:set scope="session" value="1" var="a"></c:set>//赋值
<c:remove var="a" scope="session"/>//移除
<c:out value="${a}"></c:out>//打印
</body>
</html>
利用JSTL进行If()判断
<%@page import="com.pojo.zking.User"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
Object zh=session.getAttribute("zh");
Object pwd=session.getAttribute("pwd");
<c:if test="${a==null}">
<h1>哈哈</h1>
</c:if>
</body>
</html>
利用JSTL对用户数组进行遍历
<%@page import="com.pojo.zking.User"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Object zh=session.getAttribute("zh");
Object pwd=session.getAttribute("pwd");
List<User> user1=new ArrayList<>();
for(int i=1;i<=5;i++){
User u=new User();
u.setZh("aa");
u.setPwd("bb");
user1.add(u);
}
pageContext.setAttribute("user", user1);
%>
<table>
<c:forEach items="${user}" var="b">
<tr>
<th>用户名</th>
<th>密码</th>
</tr>
<tr><td>${b.zh}</td>
<td>${b.pwd}</td>
</tr>
</c:forEach>
</table>
</body>
</html>