1. 程式人生 > >jsp學習JSTL核心標籤庫

jsp學習JSTL核心標籤庫

記住!!使用JSTL之前一定要匯入相關的jar包

jstl標籤庫需要兩個包jstl.jar和standard.jar

1.多用途核心標籤

 1.1用於顯示的<c:out> ,<c:out>標籤是一個最常用的標籤,用於在JSP中顯示資料。

 NewFile.jsp程式碼如下:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSTL Hello World!</title>
</head>
<body>
<c:out value="Hello World"></c:out>
</body>
</html>

執行結果:

     

 

1.2用於賦值的<c:set>標籤 ,<c:set>標籤用於為變數或Javabean中的變數屬性賦值的工作。

演示程式碼如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:set value="this is andy" var="oneString"></c:set>
${oneString}
//該示例將名為"oneString"的變數賦值為"this is andy",其中作用範圍為page。
</body>
</html>

執行結果:

              

1.3用於刪除的<C:remove>標籤  ,<C:remove>標籤用於刪除存在於scope中的變數。

  NewFile.jsp程式碼如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:remove var="sampleValue" scope="session"></c:remove>
${sessionScope.sampleValue}
//該示例將存在於Session中名為"sampleValue"的變數刪除。下一句EL表示式顯示該變數時,該變數已經不存在。
</body>
</html>

1.4異常捕獲的<C:catch>標籤,<C:catch>標籤允許在jsp也面中捕獲異常。它包含一個var屬性,是一個描述異常的變數。

NewFile.jsp程式碼如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:catch>${param.sampleSingleValue[9]==3}</c:catch>
${err}
//當"${param.sampleSingValue[9]==3}"表示式有異常時,可以從var屬性"err"得到異常的內容,通常判斷"err"是否為null來決定錯誤資訊的提示。
</body>
</html>

2.條件控制標籤

2.1用於判斷的<c:if>標籤。<c:if>標籤用於簡單的條件語句。

下面看一個示例:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:if test="${paramValue.sampleValue[2]==12}" var="visits" >
    It is 12
</c:if><br>
${visits}
//判斷request請求提交的傳入控制元件陣列引數中,下標為“2”的控制元件內容是否為“12”,若為12則
//顯示“It is 12”。判斷結果儲存在page範圍中的“visits”變數中。
</body>
</html>

2.2複雜的判斷<c:choose>、<c:when>、<c:otherwise>標籤  

    這三個標籤實現複雜條件判斷語句,類似“if,else if”的條件語句。

    1.<c:choose>標籤沒有屬性,可以被認為是父標籤,<c:when>、<c:otherwise> 

      將作為其子標籤來使用。

    2.<c:when>標籤等價於“if”語句,它包含一個test屬性,該屬性表示需要判斷的條件。

    3.<c:otherwise>標籤沒有屬性,它等價於“else”語句。

下面看一個複雜條件語句的示例:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:choose>
<c:when test="${paramValues.sampleValue[2]==11}">
    not 12 not 13,it is 11
</c:when>
<c:when test="${paramValues.sampleValue[2]==12}">
    not 11 not 13,it is 12
</c:when>
<c:when test="${paramValues.sampleValue[2]==13}">
   not 11 not 12,it is 13
</c:when>
<c:otherwise>
    not 11、12、13
</c:otherwise>
</c:choose>
//判斷request請求提交的傳入控制元件陣列引數中,下標為“2”控制元件內容是否為“11”或“12”或“13”,並根據判斷結果顯示各自的語句,若都不是則顯示“not 11、12、13”
</body>
</html>

2.3<c:import>檢索一個絕對或相對 URL,然後將其內容暴露給頁面

  <c:import>標籤提供了所有<jsp:include>行為標籤所具有的功能,同時也允許包含絕對URL。

  舉例來說,使用<c:import>標籤可以包含一個FTP伺服器中不同的網頁內容。

  語法格式:

<c:import
   url="<string>"
   var="<string>"
   scope="<string>"
   varRender="<string>"
   context="<string>"
   charEncoding="<string>"/>

 <c:import>的屬性:

    

  下面來看示例: 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>c:import 標籤例項</title>
</head>
<body>
<c:import var="data" url="http://www.baidu.com"/>
<c:out value="${data}"/>
</body>
</html>

3.迴圈控制標籤

 3.1迴圈<c:forEach>標籤

    基礎迭代標籤,接受多種集合型別為迴圈控制標籤。

<c:forEach>標籤有如下屬性:

  

  <c:forEach>語法格式:

<c:forEach
    items="<object>"
    begin="<int>"
    end="<int>"
    step="<int>"
    var="<string>"
    varStatus="<string>">

    ...

<c:forEach>示例:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>c:forEach 標籤例項</title>
</head>
<body>
<c:forEach var="i" begin="1" end="5">
   Item <c:out value="${i}"/><p>
</c:forEach>
</body>
</html>

 執行結果: 

Item 1
Item 2
Item 3
Item 4
Item 5

3.2分隔字元的<c:forTokens>標籤

   <c:forTokens>標籤可以根據某個分隔符指定字串,相當於Java.util.StirngTokenizer類。

  <c:forTokens>標籤與<c:forEach>標籤有相似的屬性,不過<c:forTokens>還有另一個屬性:

屬性 描述 是否必要 預設值
delims 分隔符

 

<c:forTokens>語法格式:

<c:forTokens
    items="<string>"
    delims="<string>"
    begin="<int>"
    end="<int>"
    step="<int>"
    var="<string>"
    varStatus="<string>">

<c:forTokens>示例:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>c:forTokens 標籤例項</title>
</head>
<body>
<c:forTokens items="google,runoob,taobao" delims="," var="name">
   <c:out value="${name}"/><p>
</c:forTokens>
</body>
</html>

執行結果:

google
runoob
taobao