二.JSP標簽
作用:
1.流程判斷
2.跳轉頁面
....
分類:
1.內置標簽:不需要在JSP頁面導入標簽
2.jstl標簽:需要在JSP頁面中導入標簽
3.自定義標簽:開發者自定義,需要在JSP頁面導入標簽
動態標簽:
1.轉發標簽:<jsp:forWard/>
2.參數標簽:<jsp:pararm/>
3.包含標簽:<jsp:include/>
1.內置標簽
- 轉發標簽<jsp:forWard/>
index.jsp主要代碼
<jsp:forward page="index2.jsp">
<jsp:param name="mahan" value="huan"></jsp:param>
</jsp:forward>
index2.jsp
<%--
Created by IntelliJ IDEA.
User: pc
Date: 17-4-14
Time: 下午4:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%=request.getParameter("mahan")%>
</body>
</html>
- 包含標簽<jsp:include/>
index.jsp主要代碼
<jsp:include page="head.jsp"></jsp:include>
上面是頭部
head.jsp代碼
<%--
Created by IntelliJ IDEA.
User: pc
Date: 17-4-14
Time: 下午4:46
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>我是頭部</h1>
</body>
</html>
2.jstl標簽
全名(java standard tag libarary) java標準標簽庫
1.核心標簽庫(C標簽庫)--重點
2.國際哈化標簽庫(fmt標簽庫)
3.EL函數庫(fn函數庫)
4.XML標簽庫(x標簽庫)--次要
5.sql標簽庫(sql標簽庫)--次要
- 核心標簽庫
保存數據:
<c:set></c:set>
獲取數據:
<c:out value=""></c:out>
單條件判斷:
<c:if test=""></c:if>
<多條件判斷>
<c:choose></c:choose>
<c:when test=""></c:when>
<c:otherwise></c:otherwise>
循環數據:
<c:forEach></c:forEach>
<c:forTokens items="" delims="">
</c:forTokens>
重定向 :
<c:redirect></c:redirect>
1)導入jstl支持的jar包:下載地址 密碼: g853
2)導入標簽庫
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
3)標簽
a.保存數據:<c:set></c:set>.
默認將數值存入page域中
<c:set var="mahuan" value="huanhuan"></c:set>
${mahuan}
scope="域對象"(page,request,session,application)
<c:set var="mahuan" value="huanhuan" scope="request"></c:set>
${requestScope.mahuan}
b.獲取數據:<c:out value=""></c:out>
從域中獲取數值
<c:set var="mahuan" value="huanhuan" scope="request"></c:set>
<c:out value="${mahuan}"></c:out>
default="默認值" 當值為null時顯示默認值
escapeXml=" "(true false)是否對value的值進行轉義默認轉義(true)
<%
String str =null;
pageContext.setAttribute("aaa",str);
%>
<c:out value="${aaa}" default="<h2>默認值</h2>" escapeXml="false"></c:out>
c.單條件判斷:<c:if test=""></c:if>
test=" "(true false)判斷是否要輸出
<c:if test="true">
判斷輸出
</c:if>
<c:if test="${30>22}">
判斷輸出
</c:if>
d.<多條件判斷>
<c:choose></c:choose>
<c:when test=""></c:when>
<c:otherwise></c:otherwise>
<c:set var="mahuan" value="78" ></c:set>
<c:choose>
<c:when test="${mahuan>90 && mahuan<=100}">
特等獎
</c:when>
<c:when test="${mahuan>80 && mahuan<=90}">
一等獎
</c:when>
<c:when test="${mahuan>70 && mahuan<=80}">
二等獎
</c:when>
<c:when test="${mahuan>60 && mahuan<=70}">
三等獎
</c:when>
<c:otherwise>
參賽獎
</c:otherwise>
</c:choose>
e.循環數據:<c:forEach></c:forEach>
begin= " " 從那個元素開始遍歷,默認從0開始
end=" " 到那個數據結束,默認到最后一個結束
step=“ ” 步長 默認為1
items=“ ” 需要遍歷的數據集合
var=“ ” 每個元素的名稱
varStatus= “ ” 當前正在遍歷元素的狀態對象(count屬性:當前位置,從1開始)--序列
list集合:
<%
List<Student> list= new ArrayList<Student>();
list.add(new Student("張三","21"));
list.add(new Student("小明","32"));
list.add(new Student("小花","44"));
pageContext.setAttribute("list",list);
%>
<c:forEach begin="0" end="2" items="${list}" step="1" var="Student" varStatus="varsta">
序號:${varsta.count} 姓名: ${Student.name} 年齡:${Student.age}<br>
</c:forEach>
map集合:
<%
Map<String ,Student> map = new HashMap<String,Student>();
map.put("011",new Student("張飛","44"));
map.put("012",new Student("小花","47"));
map.put("013",new Student("小明","52"));
pageContext.setAttribute("map",map);
%>
<c:forEach begin="0" end="2" items="${map}" step="1" var="Student" varStatus="varsta">
序號:${varsta.count} 編號${Student.key} 姓名: ${Student.value.name} 年齡:${Student.value.age}<br>
</c:forEach>
f.<c:forTokens items="" delims=""> </c:forTokens>
循環特殊字符串
<%
String sr = "ma-huan-huan-ni-hao";
pageContext.setAttribute("huan",sr);
%>
<c:forTokens items="${huan}" delims="-" var="s">
${s}<br>
</c:forTokens>
g.<c:redirect></c:redirect>
重定向
<c:redirect url="http://www.lxweimin.com/u/ac5fd281184c"></c:redirect>
-
完
文集:JavaEE--學習筆記