標籤:token pass 而在 dir for ons 翻譯 -- word
1、內建標籤(動作標籤)
內建標籤不需要再jsp頁面匯入標籤
1)、forward:請求的轉寄,格式如下
<%-- 作用與這個相同 <%request.getRequestDispatcher("/forward2.jsp?name=gqxing").forward(request, response); %> --%> <jsp:forward page="/forward2.jsp"> <jsp:param value="gqxing" name="name"/> <jsp:param value="123456" name="password"/> </jsp:forward>
2)、include標籤:用於包含頁面,
包含與被包含的頁面先各自翻譯成java源檔案,然後再運行時合并在一起。(先翻譯再合并),動態包含
格式如下:
<jsp:include page="/common/header.jsp"></jsp:include><hr> 這是網頁的身體部分<br>
但它與前面學的include指令有一定的區別
(1)、格式區別:
<jsp:include page="/common/header.jsp"></jsp:include><%@include file="/common/header.jsp" %>
(2)、參數傳遞區別:
動態包含可以傳遞參數,靜態傳遞不能傳遞資源
<jsp:include page="/common/head.jsp"> <jsp:param value="gqxing" name="name"/> </jsp:include><hr> 這是網頁的身體部分<br>
<h2>我是網頁的頭部,我只有一句話</h2> <%=request.getParameter("name") %>
(3)、原理不同:
靜態包含:先合并,再翻譯, 動態資源:先翻譯後合并
在靜態包含中只有首頁(網頁的身體,包含它的頁)被編譯成了java檔案,同時被包含的頁面被直接合并在了首頁的身體部分,而在動態包含中,卻沒有這個特性,被包含頁和首頁都被一同編譯了,同時在首頁中通過方法去掉用被包含頁,從而達到合并的目的,合并方法如下:
org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "/common/header.jsp", out, false);
2、jstl(java standard tag libarary)java標準標籤庫
(1)、核心標籤庫:
標籤的匯入:具體
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
(1)set標籤,儲存資料
<%--set標籤儲存資料(資料儲存到域對象,預設儲存page域中) --%> <c:set var="name" value="gqxing" scope="request"></c:set> ${requestScope.name}
(2)、out標籤,擷取資料
<%--out標籤:擷取資料 default:當value值為空白的時候使用預設值 而單純的el運算式沒有這個功能‘ escapeXml="":是否對value值進行轉移,預設為true,即進行轉義 --%> <c:out value="${name}" default="<h3>標籤</h3>" escapeXml="false"></c:out>
(3)、if標籤:單條件的判斷
<%--if 標籤:單條件判斷 --%> <c:if test="${10>5}"> <h2>條件成立</h2> </c:if> <c:if test="${!empty name }"> 是空值 </c:if>
(4)、-choose+when+otherwise標籤,多條件判斷
<%--choose+when+otherwise標籤,多條件判斷 --%> <c:set var="score" value="95"></c:set> <c:choose> <c:when test="${score>=90 && score<=100}">優秀</c:when> <c:when test="${score>=80 && score<90}">良好</c:when> <c:when test="${score>=60 && score<80}">及格</c:when> <c:otherwise>不及格</c:otherwise> </c:choose>
(5)、foreach 迴圈
首先對List集合進行迴圈遍曆
<%--foreach 迴圈 begin:指定從哪開始,預設為0 end:指定的終止位置 step:指定的步長 形如for(i=1;i<10;i++)的i++ items:需要遍曆的資料,通常是集合 var:每個元素起的名稱,為了在迴圈體中可以取用 varStatus:當前正在遍曆元素的狀態物件。(count屬性:當前位置,從1開始) --%> <% List<Student> list =new ArrayList<Student>(); list.add(new Student("gqx",21)); list.add(new Student("gqxing",22)); list.add(new Student("guoqingxing",22)); pageContext.setAttribute("list", list); %> <c:forEach begin="0" end="2" step="1" items="${list}" var="student" varStatus="varSta"> 序號:${varSta.count}——姓名:${student.name}——年齡:${student.age}<br> </c:forEach>
對map索引值對進行遍曆
<% //MAP Map<String,Student> map=new HashMap<String,Student>(); map.put("001", new Student("gqx",21)); map.put("002", new Student("gqxing",22)); map.put("003", new Student("guoqingxing",22));//放入到域中去pageContext.setAttribute("map", map); %> <c:forEach begin="0" end="2" step="1" items="${map }" var="entry" varStatus="varStar"> 序號:${entry.key }——姓名:${entry.value.name }——年齡:${entry.value.age }<br> </c:forEach> <hr>
(6)、forToken:迴圈特殊的字串
<%--forToken:迴圈特殊的字串 --%> <% String str="java-php-net-photoship-c++"; pageContext.setAttribute("str", str); %> <c:forTokens items="${str }" delims="-" var="s" varStatus="varStar"> ${varStar.count }——${s }<br> </c:forTokens>
(7)、redirect:重新導向
<%--redirect:重新導向 --%> <c:redirect url="http://www.baidu.com"> </c:redirect>
Jsp的內建標籤和jstl標籤