天天看點

第九節、JSTL标簽庫

文章目錄

      • JSTL标簽概述
      • JSTL的core标簽庫
      • JSTL的fmt标簽
      • JSTL的fn标簽庫
      • JSTL的SQL标簽庫
      • JSTL的XML标簽庫

JSTL标簽概述

分類:

core标簽庫(核心标簽庫),包括有通用标簽(輸出标簽)、流控制标簽和循環控制标簽等。

fmt标簽庫,格式化、國際化标簽庫。

fn标簽庫,函數标簽庫。

XML标簽庫,關于XML操作的标簽庫。

SQL标簽庫,操作資料庫的标簽庫。

JSTL的core标簽庫

<c:set>标簽用于在某個範圍中設定某個值,這個範圍可以是request、page、session、application。

<c:set value=“表達式” var=“varname”[scope=“request|page|session|application”]>

<c:out>标簽用于把表達式的結果輸出到頁面中,escapeXML預設值是true,将特殊字元轉換:<轉換成&lt,>轉換為&gt。

<c:out value="表達式“[escapeXML=“true|false”]>

<c:if>标簽用于條件判斷,var參數是條件的執行結果(true/false)。

<c:if test=“判斷條件” [var=“varname”][scope=“request|page|session|application”]>

<c:choose/>、<c:when/>、<c:otherwise/>标簽。當<c:when/>判斷為假時,才會執行<c:otherwise/>标簽。

<c:choose>

----<c:when test=”表達式”>

---------表達式為真時執行的語句

----</c:when>

----[<c:otherwise> 表達式為假時執行的語句]

</c:choose>

第九節、JSTL标簽庫
第九節、JSTL标簽庫
第九節、JSTL标簽庫
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML >
<html>
  <head>
    <title>&lt;c:forEach&gt;标簽使用例子</title>
  </head>
  
  <body>
      <%
           List<String> nameLists = new ArrayList<String>();
           nameLists.add("Toms");
           nameLists.add("Smith");
           nameLists.add("John");
           nameLists.add("Anna");
           nameLists.add("James");
           nameLists.add("Roses");
           nameLists.add("Bruce");
           request.setAttribute("nameLists",nameLists);
       %>
      <div style="text-align: center;">
                             輸出集合中的内容:<hr/>
           <c:forEach items="${nameLists}" var="name" varStatus="currentStatus">
		                 目前元素為:<c:out value="${name}"/>&nbsp;&nbsp;
		                 目前元素索引号為:<c:out value="${currentStatus.index}"/>&nbsp;&nbsp;
		                 目前疊代數為:<c:out value="${currentStatus.count}"/>&nbsp;&nbsp;
              <c:if test="${currentStatus.first}">第一次循環操作</c:if>
              <c:if test="${currentStatus.last}">最後一次循環操作</c:if>
                 <hr/>
           </c:forEach>
      </div>
  </body>
</html>
           
第九節、JSTL标簽庫

<c:forTokens>标簽用于對字元串進行分隔,類似Java中split方法。

<c:forTokens items=“字元串” delims=“分隔符” [var=“别名”][varStatus=”varstatusName”] [begin=”開始”] [end=”結束”] [step=”步長”]>

----java代碼,HTML代碼等

</c:forTokens>

<c:remove var=“varname”[scope=“request|page|session|application”]>
<c:catch>類似于Java中try…catch
第九節、JSTL标簽庫
<c:import>标簽的作用是把目前JSP頁面之外的靜态和動态檔案導入進來,甚至可以是其他網站的檔案。而 < < <jsp:include > > >隻能導入目前JSP頁面在同一Web應用下的檔案。
第九節、JSTL标簽庫
第九節、JSTL标簽庫
第九節、JSTL标簽庫
第九節、JSTL标簽庫
第九節、JSTL标簽庫

JSTL的fmt标簽

第九節、JSTL标簽庫
第九節、JSTL标簽庫
第九節、JSTL标簽庫
第九節、JSTL标簽庫
第九節、JSTL标簽庫
第九節、JSTL标簽庫
第九節、JSTL标簽庫
第九節、JSTL标簽庫
第九節、JSTL标簽庫
第九節、JSTL标簽庫
第九節、JSTL标簽庫
第九節、JSTL标簽庫

JSTL的fn标簽庫

第九節、JSTL标簽庫
第九節、JSTL标簽庫
第九節、JSTL标簽庫

JSTL的SQL标簽庫

<%@ page import="java.io.*,java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ page import="java.util.Date,java.text.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<html>
<head>
   <title>sql标簽綜合示例</title>
</head>
<body>
<!-- 設定資料源-->
<sql:setDataSource var="test" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost:3306/TEST"
     user="root"  password="root"/>
<!-- 将使用者的年齡增加2歲 -->
<sql:update dataSource="${test}"   var="updatecount">    
   update users set user_age=user_age+?
   <c:set value="2" var="count"/>
   <sql:param value="${count}"/>
</sql:update>
<!-- 給id為1的使用者設定日期 -->
<%
Date nowdate = new Date();
int userId = 1;
%>
<sql:update dataSource="${test}"   var="updatecount2">    
   UPDATE users SET createtime = ? WHERE Id = ?
   <sql:dateParam value="<%=nowdate%>" type="timestamp" />
   <sql:param value="<%=userId%>" />
</sql:update>
<!-- 查詢資料-->
<sql:query dataSource="${test}" var="result" 
   sql="SELECT * from users;" >
</sql:query>
<!--顯示資料  -->
<table border="1" width="100%">
<tr>
   <td colspan="7" align="center">
       共查詢${result.rowCount}條使用者記錄
   </td>
</tr>
<tr>
   <th>使用者ID</th>
   <th>使用者姓名</th>
   <th>使用者性别</th>
   <th>使用者年齡</th>
   <th>聯系電話</th>
   <th>出身地</th>
   <th>建立日期</th>
</tr>
<c:forEach var="user" items="${result.rows}">
<tr>
   <td><c:out value="${user.id}"/></td>
   <td><c:out value="${user.user_name}"/></td>
   <td><c:out value="${user.user_sex}"/></td>
   <td><c:out value="${user.user_age}"/></td>
   <td><c:out value="${user.user_phone}"/></td>
   <td><c:out value="${user.user_address}"/></td>
   <td><fmt:formatDate type="both" value="${user.createtime}" var="formatUsertime"/>
       <c:out value="${formatUsertime}"></c:out></td>
</tr>
</c:forEach>
</table>
<%-- <sql:transaction dataSource="" isolation="">
 
 </sql:transaction>--%>
</body>
</html>
           
第九節、JSTL标簽庫
第九節、JSTL标簽庫

JSTL的XML标簽庫

第九節、JSTL标簽庫
第九節、JSTL标簽庫
第九節、JSTL标簽庫
第九節、JSTL标簽庫
第九節、JSTL标簽庫
第九節、JSTL标簽庫
第九節、JSTL标簽庫

繼續閱讀