Jstl簡介
鑒于目前強大的struts標簽,貌似其已經成為
主流````而以前在頁上常用的Jstl標記性語言用得越來越少,但是作為一個新手,面對前人寫的程序,沒有對JSTL作一個基本的了解是不行的,因為,我決定對JSTL作一個簡單的了解,至少能認識是什么東西吧,不要求完全掌握。
一、我的第一個jstl程序
運行ide:myeclipse
首先建立一個web項目,再在項目中添加jstl,即myeclise的add jstl```````````````.
或者在你的web中加入jstl.jar standard.jar
Test.java
1 <%@ page language="java" pageEncoding="GBK"%>
2 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <html>
5 <head>
6 <title>My JSP 'test.jsp' starting page</title>
7 </head>
8 <body>
9 <c:out value="歡迎使用你的第一個測試頁面"/>
10 <br>
11 <c:out value="``````````````````````````"/>
12 <br>
13 <c:out value="你的名字"/>
14 </body>
15 </html>
16
二、關于jstl
JSTL所提供的函數標簽庫主要分為以下5類。
類別
|
內容
|
核心標簽庫
|
提供定制操作、以及執行頁面內容的迭代和條件操作,還提供了用來生成和操作URL的標簽。
|
XML標簽庫
|
提供了用來 操作以XML表示的數據的標簽。
|
格式化/國際化(i18n)標簽庫
|
定義了用來格式化數據(尤其是數字和日期)的操作的標簽,這些標簽還支持使用本地化資源進行JSP頁面的國際化。
|
數據庫標簽庫
|
定義了用來查詢關系數據庫操作的標簽。
|
函數標簽庫
|
利用EL的Function所實現出來的,主要用于處理字符串。
|
表 7-1
JSTL 前置名稱 URI 范 例
核心標簽庫 c http://java.sun.com/jsp/jstl/core <c:out>
I18N 格式標簽庫 fmt http://java.sun.com/jsp/jstl/xml <fmt:formatDate>
SQL標簽庫 sql http://java.sun.com/jsp/jstl/sql <sql:query>
XML標簽庫 xml http://java.sun.com/jsp/jstl/fmt <x:forBach>
函數標簽庫 fn http://java.sun.com/jsp/jstl/functions <fn:split>
三.關于每個庫的實例
對于每一個標簽的用法和標簽中的屬性我們在這不作說明,有興趣的可以參越相關的文檔。
(1)核心標簽庫
核心標簽庫分類
分類
|
功能
|
標簽
|
Core
|
表達式相關
|
out set remove catch
|
流程控制
|
if choose when otherwise
|
迭代
|
forEach forTokens
|
URL
|
import param url param rediect param
|
表達式相關實例:
1 <%@ page contentType="text/html;charset=GB2312" %>
2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3
4 <html>
5 <head>
6 <title>表達式相關的標簽</title>
7 </head>
8 <body>
9
10 <h2>使用<c:out value="<c:out> <c:set> <c:remove>" />的例子</h2>
11 <hr>
12 <c:set scope="page" var="number">
13 <c:out value="${5}"/>
14 </c:set>
15 <br>
16 <c:set scope="request" var="number">
17 <c:out value="${5}"/>
18 </c:set>
19 <br>
20 <c:set scope="session" var="number">
21 <c:out value="${5}"/>
22 </c:set>
23
24 各范圍number變量的初始值</p>
25
26 pageScope.number =<c:out value="${pageScope.number}" default="No Data" />
27 <br>
28 requestScope.number =<c:out value="${requestScope.number}" default="No Data" />
29 <br>
30 sessionScope.number =<c:out value="${sessionScope.number}" default="No Data" />
31 <br>
32
33 <p><c:out value='執行<c:remove var="number" />之后'/></p>
34
35 <c:remove var="number" />
36
37 pageScope.number =<c:out value="${pageScope.number}" default="No Data" />
38 <br>
39 requestScope.number =<c:out value="${requestScope.number}" default="No Data" />
40 <br>
41 sessionScope.number =<c:out value="${sessionScope.number}" default="No Data" />
42 <br>
43 </body>
44 </html>
45
流程控制實例:
1 <%@ page contentType="text/html;charset=GB2312" %>
2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3
4 <html>
5 <head>
6 <title>JSTL中用于流程控制的標簽</title>
7 </head>
8 <body>
9
10 <h2>標簽c:if的使用</h2>
11 <c:if test="${param.username == 'Tom'}" var="condition" scope="session">
12 您好,Tom
13 </c:if>
14 <c:if test="${param.username == 'Jerry'}" var="condition" scope="session">
15 您好,Jerry
16 </c:if>
17 <c:if test="${param.username == 'Mike'}" var="condition" scope="session">
18 您好,Mike
19 </c:if>
20 <c:if test="${param.username == 'Ben'}" var="condition" scope="session">
21 您好,Ben
22 </c:if>
23
24 <h2>標簽c:choose c:when c:otherwise使用</h2>
25 <c:choose>
26 <c:when test="${param.username == 'Tom'}">
27 您好,Tom
28 </c:when>
29 <c:when test="${param.username == 'Jerry'}">
30 您好,Jerry
31 </c:when>
32 <c:when test="${param.username == 'Mike'}">
33 您好,Mike
34 </c:when>
35 <c:otherwise>
36 您好,Ben
37 </c:otherwise>
38 </c:choose>
39 </body>
40 </html>
41
迭代實例:
1 <%@ page contentType="text/html;charset=GB2312" %>
2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3
4 <html>
5 <head>
6 <title>使用c:forEach和c:forTokens標簽</title>
7 </head>
8 <body>
9
10 <h2><c:out value="<c:forEach> 的用法" /></h2>
11
12 <%
13 String atts[] = new String [4];
14 atts[0]="您好!";
15 atts[1]="歡迎您!";
16 atts[2]="您已經成功使用c:forEach標簽.";
17 atts[3]="____________________________柳宗元";
18 request.setAttribute("atts", atts);
19 %>
20
21 <c:forEach items="${atts}" var="item" >
22 ${item}</br>
23 </c:forEach>
24
25 <h2><c:out value="<c:forTokens> 的用法" /></h2>
26
27 <%
28 String postcode = "34:5873:9898:001";
29 request.setAttribute("mypostcode", postcode);
30 %>
31
32 <c:forTokens items = "${mypostcode}" delims = ":" var = "item">
33 ${item}
34 </c:forTokens>
35
36 </body>
37 </html>
38
結 果:
<c:forEach> 的用法
您好!
歡迎您!
您已經成功使用c:forEach標簽.
____________________________柳宗元
<c:forTokens> 的用法
34 5873 9898 001
URL標簽比較簡單,不用再做介紹。
(2)數據庫標簽實例
1 <%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
2
3 <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
4
5 <html>
6
7 <head>
8 <title>JSTL: SQL in action </title>
9 </head>
10 <body bgcolor="#FFFFFF">
11 <h1>SQL Update Execution</h1>
12 <sql:setDataSource
13
14 var="example"
15 driver="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=BookDB"
16 url="com.microsoft.jdbc.sqlserver.SQLServerDriver "
17 user="sa"
18 password=""
19 />
20 <hr>
21
22 <sql:transaction dataSource="${example}">
23
24 <sql:update var="newTable">
25 create table mytable (
26
27 nameid int primary key,
28 name varchar(80)
29 )
30 </sql:update>
31
32 <h2>在表中插入三行記錄</h2>
33 <sql:update var="updateCount">
34 INSERT INTO mytable VALUES (1,'zhangshan')
35
36 </sql:update>
37 <sql:update var="updateCount">
38 INSERT INTO mytable VALUES (2,'lishi')
39 </sql:update>
40 <sql:update var="updateCount">
41 INSERT INTO mytable VALUES (3,'wangwu')
42 </sql:update>
43
44 <p>插入三行結束</p>
45 <sql:query var="deejays">
46 SELECT * FROM mytable
47 </sql:query>
48
49 </sql:transaction>
50
51 <%-- An example showing how to populate a table --%>
52 <table border="1">
53
54 <%-- Get the column names for the header of the table --%>
55 <c:forEach var="columnName" items="${deejays.columnNames}">
56 <th><c:out value="${columnName}"/></th>
57 </c:forEach>
58
59 <%-- Get the value of each column while iterating over rows --%>
60 <c:forEach var="row" items="${deejays.rows}">
61 <tr>
62 <c:forEach var="column" items="${row}">
63 <td><c:out value="${column.value}"/></td>
64 </c:forEach>
65
66 </tr>
67 </c:forEach>
68 </table>
69 <h2>更新表中的一行記錄</h2>
70
71 <sql:update var="updateCount" dataSource="${example}">
72 UPDATE mytable SET name=? <sql:param value="Scott Tiger"/> WHERE nameid=1
73 </sql:update>
74 <%-- The Value for sql:param can be obtained from the JSP parameters --%>
75 <p>更新一行記錄成功</p>
76
77 <sql:query var="deejays" dataSource="${example}">
78 SELECT * FROM mytable
79 </sql:query>
80 <%-- Yet another example showing how to populate a table --%>
81 <table border="1">
82 <c:forEach var="row" items="${deejays.rows}" varStatus="status">
83 <%-- Get the column names for the header of the table --%>
84 <c:choose>
85 <c:when test="${status.count == 1}">
86 <%-- Each row is a Map object key'd by the column name --%>
87 <tr>
88 <c:forEach var="metaData" items="${row}">
89 <th><c:out value="${metaData.key}"/></th>
90 </c:forEach>
91 </tr>
92 </c:when>
93 </c:choose>
94 <tr>
95 <c:forEach var="column" items="${row}">
96 <%-- Get the value of each column while iterating over rows --%>
97 <td><c:out value="${column.value}"/></td>
98 </c:forEach>
99 </tr>
100 </c:forEach>
101 </table>
102 <h2>刪除表中的第二條記錄</h2>
103
104 <sql:update var="updateCount" dataSource="${example}">
105 DELETE FROM mytable WHERE nameid=2
106 </sql:update>
107
108 <p>刪除完成</p>
109 <sql:query var="deejays" dataSource="${example}">
110 SELECT * FROM mytable
111 </sql:query>
112 <%-- Yet another example showing how to populate a table --%>
113 <table border="1">
114 <c:forEach var="row" items="${deejays.rows}" varStatus="status">
115 <%-- Get the column names for the header of the table --%>
116 <c:choose>
117 <c:when test="${status.count == 1}">
118 <%-- Each row is a Map object key'd by the column name --%>
119 <tr>
120 <c:forEach var="metaData" items="${row}">
121 <th><c:out value="${metaData.key}"/></th>
122 </c:forEach>
123 </tr>
124 </c:when>
125 </c:choose>
126 <tr>
127 <c:forEach var="column" items="${row}">
128 <%-- Get the value of each column while iterating over rows --%>
129 <td><c:out value="${column.value}"/></td>
130 </c:forEach>
131 </tr>
132 </c:forEach>
133 </table>
134 <sql:update var="newTable" dataSource="${example}">
135 drop table mytable
136 </sql:update>
137 </body>
138 </html>
139
(3)sql和xml標簽
關于這二個標簽,有興趣的可以參考ibm的學習文章
www.ibm.com/developerworks/cn/java/j-jstl0520/
看了這篇文章對jstl有一個了解了吧,其實現在不用去深入學習,純屬個人觀點,呵呵!
由于現在用得不算太多,整理得不詳細,望諒!如果有需要更詳細的,留下你的email!
posted on 2009-04-16 10:03
重慶理工小子 閱讀(2428)
評論(6) 編輯 收藏 所屬分類:
Jsp基礎編程