1.內(nèi)置標(biāo)簽
a.請(qǐng)求轉(zhuǎn)發(fā):
<jsp:forward/>
<jsp:pararm/>
<jsp:forward page="/index.jsp">
<jsp:pararm name="參數(shù)名" value="參數(shù)值"/>
</jsp:forward>
b.動(dòng)態(tài)頁(yè)面包含:
<jsp:include/>
< jsp:include page="index.jsp"></jsp:include>
- 注:靜態(tài)包含和動(dòng)態(tài)包含的區(qū)別
1) 語(yǔ)法不同
靜態(tài)包含語(yǔ)法:<%@inclue file="被包含的頁(yè)面"%>
動(dòng)態(tài)包含語(yǔ)法:<jsp:include page="被包含的頁(yè)面">
2)參數(shù)傳遞不同
靜態(tài)包含不能向被包含頁(yè)面?zhèn)鬟f參數(shù)
動(dòng)態(tài)包含可以向被包含頁(yè)面?zhèn)鬟f參數(shù)3)原理不同
靜態(tài)包含: 先合并再翻譯
動(dòng)態(tài)包含: 先翻譯再合并
2.JSTL標(biāo)簽
JSTL(java standard tag libarary --java標(biāo)準(zhǔn)標(biāo)簽庫(kù))
- 核心標(biāo)簽庫(kù):c標(biāo)簽庫(kù)(重要)
- 國(guó)際化標(biāo)簽庫(kù):fmt標(biāo)簽庫(kù)
- EL函數(shù)庫(kù):fn函數(shù)庫(kù)
- xml標(biāo)簽庫(kù):x標(biāo)簽庫(kù)(不常用)
- sql標(biāo)簽庫(kù):sql標(biāo)簽庫(kù)(不常用)
-
使用步驟:
1.導(dǎo)入標(biāo)簽庫(kù)jar包:
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
2.使用taglib指令導(dǎo)入標(biāo)簽庫(kù)
<%@taglib uri="http://java.sun.com/jstl/core(tld文件的uri名稱)" prefix="標(biāo)簽庫(kù)簡(jiǎn)寫" %>
3.使用
-
c標(biāo)簽庫(kù)
保存數(shù)據(jù):
<c:set></c:set>
獲取數(shù)據(jù):<c:out value=""></c:out>
單條件判斷:<c:if test=""></c:if>
多條件判斷:<c:choose></c:choose>
<c:when test=""></c:when>
<c:otherwise></c:otherwise>
循環(huán)數(shù)據(jù):<c:forEach></c:forEach>
<c:forTokens items="" delims=""></c:forTokens>
重定向:<c:redirect url=""></c:redirect>
測(cè)試代碼:
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="com.eu.login.entity.Login" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:set var="user" value="<h3>xiaoming</h3>"></c:set>賦值
<hr/>
取值:<c:out value="${user}" default="默認(rèn)值" escapeXml="false"></c:out>
<hr/>
<c:if test="${!empty user}">
<h3>true</h3>
</c:if>
<hr/>
<c:set var="score" value="10"></c:set>
<c:choose>
<c:when test="${score>=60 && score <= 100}">
合格
</c:when>
<c:when test="${score<60 && score != 0}">
不合格
</c:when>
<c:otherwise>
沒有成績(jī)
</c:otherwise>
</c:choose>
<hr/>
<%
List list = new ArrayList();
list.add(new Login("xiao1","123"));
list.add(new Login("xiao2","456"));
list.add(new Login("xiao3","789"));
list.add(new Login("xiao4","159"));
request.setAttribute("list",list);
Map<String ,String> map = new HashMap<>();
map.put("001","小明1");
map.put("002","小明2");
map.put("003","小明3");
map.put("004","小明4");
request.setAttribute("map",map);
%>
<%--
begin:從那個(gè)元素開始
end:從那個(gè)元素結(jié)束
step:步長(zhǎng)
items:需要遍歷的數(shù)據(jù)(集合)
var:每個(gè)元素的名稱
varStatus:當(dāng)前正在遍歷元素的狀態(tài)對(duì)象
--%>
<c:forEach begin="0" end="3" step="1" items="${list}" var="login" varStatus="varsta">
序號(hào):<c:out value="${varsta.count}"></c:out>
姓名:<c:out value="${login.username}"></c:out>
密碼:<c:out value="${login.password}"></c:out><br/>
</c:forEach>
<hr/>
<c:forEach items="${map}" var="entry" varStatus="varsta">
序號(hào):<c:out value="${varsta.count}"></c:out>
姓名:<c:out value="${entry.key}"></c:out>
密碼:<c:out value="${entry.value}"></c:out><br/>
</c:forEach>
<hr/>
<%
String str = "代碼-25-36-54-25";
request.setAttribute("str",str);
%>
<%--根據(jù)指定的分隔符來分隔內(nèi)容并迭代輸出--%>
<c:forTokens items="${str}" delims="-" var="st">
元素:<c:out value="${st}"></c:out><br>
</c:forTokens>
<hr>
<%--重定向--%>
<c:redirect url="index.jsp"></c:redirect>
</body>
</html>
測(cè)試結(jié)果:
-
fmt標(biāo)簽庫(kù)
圖片來自菜鳥教程 -
fn函數(shù)庫(kù)
圖片來自菜鳥教程
文章文集:JavaEE--學(xué)習(xí)筆記