javaWeb知識學習——JSTL標籤的使用

知識點彙總

JSTL全名爲:JavaServer Pages Standard Tag Library
1.提供的標籤函數庫主要分爲五大類:核心函數庫、I18N格式標籤庫
SQL標籤庫、XML標籤庫、函數標籤庫.
2。表達式操作分類中包含四個標籤<c:out><c:set><c:remove>,<c:catch>
3.流程控制包含四個標籤:<c:if><c:choose><c:when><c:otherwise>
4.迭代操作主要包含兩個標籤:<c:forEach>:可對數組,Collection,Map進行遍歷,<c:forTokens>。
5.URL操作有關的標籤:<c:import><c:redirect><c:url>
首先在lib文件下導入jstl.jar和standard.jar包

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

代碼示例如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>JSTL</title>
</head>
<body>
<%
    request.setAttribute("book","<<WEB>>");
%><br>
book:<c:out value="${requestScope.book}" default="booktitle"></c:out><br>
<c:set var="name" value="JAVA" scope="page"></c:set>
name:${pageScope.name}<br>
<c:set var="subject" value="${param.subject}" scope="session"></c:set><br>
<%
    Customer customer=new Customer();
    customer.setCard("1002");
    //request.setAttribute("customer",customer);
%><br>
<c:set target="${requestScope.customer}" property="card" value="${param.card}"></c:set>
ID:${requestScope.customer.id}
<%--移除操作--%>
<c:remove var="name" scope="page"></c:remove>
<h3>流程控制</h3>
<c:set var="age" value="20" scope="request"></c:set>
<c:if test="${requestScope.age > 18}">成人</c:if><br>
<c:if test="${param.age} > 18" var="adult" scope="request"></c:if>
isAdult:<c:out value="${requestScope.adult}"></c:out><br>
<c:choose>
    <c:when test="${param.age > 18}">青年</c:when>
    <c:when test="${param.age > 35}">壯年</c:when>
    <c:otherwise>未成年</c:otherwise>
</c:choose>
<h3>迭代操作</h3>
<c:forEach begin="1" end="10" step="2" var="i">
    ${i}
</c:forEach><br>
<%
    List<Customer> customers=new ArrayList<Customer>();
    customers.add(new Customer("tom","bj","mas","234"));
    customers.add(new Customer("jerry","bj","zds","5667"));
    customers.add(new Customer("hew","bj","mas","789"));
    request.setAttribute("customers",customers);
%><br>
<c:forEach items="${requestScope.customers}" var="cust">
    ${cust.name}--${cust.address}
</c:forEach><br>
<%
    Map<String ,Customer> custMap=new HashMap<String,Customer>();
    custMap.put("a",new Customer("tom","bj","mas","234"));
    custMap.put("b",new Customer("jerry","bj","mas","234"));
    custMap.put("c",new Customer("hew","bj","mas","234"));
    request.setAttribute("custMap",custMap);
%><br>
<c:forEach items="${requestScope.custMap}" var="cust">
    ${cust.key}-${cust.value.name}-${cust.value.address}
</c:forEach><br>
<c:set var="test" value="a,b,c.d.e.f.g" scope="request"></c:set><br>
<c:forTokens items="${requestScope.test}" delims="." var="s">
    ${s}
</c:forTokens>
<h3>URL操作的標籤</h3>
<c:import url="http://www.baidu.com"></c:import>
<%--c:redirect使當前JSP頁面重定向到指定頁面使用,
使當前JSP頁面轉發到指定頁面可以使用<jsp:forward page="/hello.jsp"></jsp:forward>
--%>
<c:redirect url="simpleTag.jsp"></c:redirect><br>
<c:url value="simpleTag.jsp" var="testUrl" scope="page">
    <c:param name="name" value="url"></c:param>
</c:url>
url:${testUrl}
</body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章