jstl
jstl簡介:
jstl的全稱:jsp standard tag library,jsp標準標簽庫
jstl的作用:
提供給java web開發人員一個標準通用的標簽函數庫
和el來取代傳統直接在頁面上嵌入java程序(scripting)的做法,以提高程序可讀性、維護性和方便性
jstl的版本:
jstl的主要版本是1.0、1.1和1.2(區別不大)
1.0版本el表達式還沒有納入官方規范
1.1和1.2版本el表達式已經納入了官方規范
jstl1.1 下載相應的jar包
jstl的下載
jstl主要由apache組織的jakarta project實現
容器必須支持servlet2.4且jsp2.0以上的版本
javaee1.4
jstl導入jar包
解壓縮后將lib中的jstl.jar、standard.jar 復制到web應用程序的web-inf\lib下
jstl標簽庫
- 核心標簽庫(core)---c(重點)
- xml(x:操作xml的標簽庫)
- sql(sql標簽庫)
- fmt(fmt:國際化標簽庫)
- jstl函數(el函數)el
jstl快速入門
導入jar包(jstl.jar和standard.jar)
其中jstl.jar是編譯后的java類文件,standard.jar定義的是標準接口
新建jsp的頁面
在頁面中引入核心標簽庫
1
|
<%@ taglib uri= "http://java.sun.com/jsp/jstl/core" prefix= "c" %> |
jstl標簽庫關于導入版本的問題
1.1或者1.2版本(使用該版本)
1.0版本(不支持el表達式)
<c:out>標簽輸出常量或者域范圍中的變量(value屬性,使用el表達式)
輸出默認值(default屬性)
默認html標簽不轉義(escapexml)
屬性名 | 是否支持el | 屬性類型 | 屬性描述 |
value | true | object | 指定要輸出的內容 |
escapexml | true | boolean |
指定是否將>、<、&、'、" 等 特殊字符進行html編碼轉換 后再進行輸出。默認值是true。 |
default | true | object | 指定如果value屬性的值為null時所輸出的默認值 |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!-- c:out 輸出數據到瀏覽器 --> <c:out value= "hello c out " ></c:out> hello c out <!-- 輸出一個變量 --> <c:set var= "m" value= "10" scope= "page" /> <c:out value= "${m}" ></c:out> ${m } <!-- 轉義html 默認轉義,通過設置escapexml 為 false 不進行轉義--> <c:out value= "<a href='xxx'>link</a>" /> ${fn:escapexml( "<a href='xxx'>link</a>" ) } <!-- 允許輸出默認值 ,如果city不存在,輸出北京--> <c:out value= "${city}" default = "北京" ></c:out> ${empty city? "北京" :city } |
在webroot下新建jstl文件夾,在文件夾下新建out.jsp
實例代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<%@ 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> <h4>傳統方式</h4> <%= "hello" %> <% int a = 10 ; request.setattribute( "name" , "xy" ); %> <%= a %> <h4>jstl的方式 </h4> <c:out value= "hello" ></c:out> <c:out value= "${name }" ></c:out> <!-- "" --> <c:out value= "${ city }" default = "北京" ></c:out> <c:out value= "<a href='#'>超鏈接</a>" escapexml= "false" /> <c:out value= "<a href='#'>超鏈接2</a>" escapexml= "true" /> </body> </html> |
<c:set>標簽
- 向4個域中存入值。(var value scope屬性)
- 設置web域中的java.util.map 類型的屬性對象或javabean類型的屬性對象的屬性(target property value屬性)
屬性名 | 是否支持el | 屬性類型 | 屬性描述 |
value | true | object | 用于指定屬性 |
var | false | string | 用于指定要設置的web域屬性的名稱 |
scope | false | string | 用于指定屬性所在的web域 |
target | true | object |
用于指定要設置屬性的對象,這個對象必須是 javabean對象或java.util.map對象 |
property | true | string | 用于指定當前要為對象設置的屬性名稱 |
在webroot/jstl下新建set.jsp
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<% @page import = "cn.itcast.vo.user" %> <%@ 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> <h4>傳統方式</h4> <% pagecontext.setattribute( "name" , "10" , pagecontext.request_scope); %> <% user user = new user(); user.setusername( "美美" ); user.setpassword( "123" ); request.setattribute( "user" , user); %> ${ user.username } <h4>jstl方式</h4> <c:set var= "i" value= "10" scope= "request" ></c:set> ${ i } <c:set target= "${ user }" property= "username" value= "小鳳" ></c:set> ${ user.username } </body> </html> |
<c:remove>標簽
<c:remoive>標簽用于刪除各種web域中的屬性
其語法格式如下:
1
|
<c:remove var= "varname" [scope= "{page|request|session|application}" ]> |
如果不設置,默認是pagecontext域范圍內查找刪除值。
用法示例:
1
2
3
4
5
6
7
|
<% request.setattribute( "age" , 20 ); // 刪除age request.removeattribute( "age" ); %> <c:set var= "age" value= "20" scope= "request" ></c:set> <c:remove var= "age" scope= "request" /> |
在webroot/jstl目錄下新建remove.jsp
實例代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<%@ 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> <h4>傳統方式</h4> <% request.setattribute( "name" , "美美" ); request.removeattribute( "name" ); %> <c:set var= "name" value= "小鳳" scope= "page" ></c:set> ${ name } <c:remove var= "name" scope= "page" /> ${name } </body> </html> |
<c:catch>標簽
- <c:catch>標簽用于捕獲嵌套在標簽中的內容拋出的異常,其語法格式如下:<c:catch [var="varname"]> nested actions </c:catch>
- var屬性用于標識<c:catch>標簽捕獲的異常對象,它將保存在page這個web域中。
關鍵代碼:
1
2
3
4
5
6
7
|
<%@ taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %> <%@ page contenttype= "text/html;charset=gb2312" %> <c: catch var="myex“ > <% 10 / 0 ; %> </c: catch > |
異常:<c:out value="${myex}" /> ${myex}<br />
異常 myex.getmessage:<c:out value="${myex.message}" /><br />
異常 myex.getcause:<c:out value="${myex.cause}" /><br />
異常 myex.getstacktrace:<c:out value="${myex.stacktrace}" />
在webroot/jstl下新建catch.jsp
實例代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<%@ 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> <h4>傳統方式</h4> <% try { } catch (exception e){ e.printstacktrace(); } %> <h4>jstl的方式</h4> <c: catch var= "e" > <% int a = 10 / 0 ; %> </c: catch > ${ e.message } </body> </html> |
<c:if>標簽
<c:if test=""> 標簽可以構造簡單的"if-then"結構的條件表達式
屬性名 | 是否支持el | 屬性類型 | 屬性描述 |
test | true | boolean | 決定是否處理標簽體中的內容的條件表達式 |
var | false | string | 用于指定將test屬性的執行結果保存到某個web域中的某個屬性的名稱 |
scope | false | string | 指定將test屬性的執行結果保存到哪個web域中 |
注意:沒有<c:else>
在webroot/jstl下新建if.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<%@ 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> <h4>傳統方式</h4> <% int a = 10 ; if (a >= 10 ){ out.print( "a >= 10" ); } else { out.print( "a < 10" ); } %> <h4>jstl方式</h4> <c:set var= "i" value= "10" scope= "page" ></c:set> <c: if test= "${ i ge 10 }" var= "x" scope= "page" > i >= 10 </c: if > <c: if test= "${ i lt 10 }" > i < 10 </c: if > ${ x } </body> </html> |
1
2
3
4
5
6
7
|
<c:choose> <c:choose>標簽用于指定多個條件選擇的組合邊界,它必須與<c:when>和<c:otherwise>標簽一起使用。使用<c:choose>,<c:when>,<c:otherwise>三個標簽,可以構造類似于 "if-else if-else" 的復雜條件判斷結構 <%@ taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %> <%@ page contenttype= "text/html;charset=gb2312" %> <c:set value= "${param.count}" var="count“ /> pagecontext(count, 2 ) <c:choose> <c:when test= "${count == 0}" > |
對不起,沒有符合您要求的記錄。
</c:when>
<c:otherwise>
符合您要求的記錄共有${count}條.
</c:otherwise>
</c:choose>
在webroot/jstl目錄下新建choose.jsp
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<%@ 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> <h4>獲取參數</h4> <%= request.getparameter( "username" ) %> <h4>傳統方式</h4> <% int a = 10 ; if (a >= 10 ){ out.print( "a >= 10" ); } else if (a < 10 ){ out.print( "a < 10" ); } else { out.print( "其他" ); } %> <h4>jstl方式</h4> <c:set var= "i" value= "10" scope= "page" ></c:set> <c:choose> <c:when test= "${ i ge 10 }" > i >= 10 </c:when> <c:when test= "${ i lt 10 }" > i < 10 </c:when> <c:otherwise> 其他 </c:otherwise> </c:choose> </body> </html> |
<c:foreach>標簽
<c:foreach>標簽用于對一個集合對象中的元素進行循環迭代操作,或者按指定的次數重復迭代執行標簽體中的內容
屬性名 | 是否支持el | 屬性類型 | 屬性描述 |
var | false | string | 指定將當前迭代到的元素保存到page這個域中的屬性名稱 |
varstatus | false | string | 記住用于保存迭代信息的對象 |
items | true | 任何支持的類型 | 將要迭代的集合對象 |
begin | true | int |
如果指定items屬性,就從集合中的第begin個元素開始進行迭代 ,begin的索引值從0開始編號,如果沒有指定items屬性,就從 begin指定的值開始迭代,直到end值時結束迭代 |
end | true | int | 與begin屬性類似 |
step | true | int | 指定迭代的步長,即迭代因子的迭代增量 |
<c:foreach>遍歷集合
遍歷數組或者集合:
var:代表遍歷的每一個元素
items:要迭代的集合對象
獲取遍歷的內容:${ xxx }
遍歷map集合:
var:代表key與value的關系entry
items:要迭代的map集合
獲取遍歷的內容:${ xxx.key } ----${ xxx.value }
遍歷對象集合:
var:代表遍歷的每一個對象
items:要迭代的集合
獲取遍歷的內容: ${ xxx.對象的屬性 }
<c:foreach>迭代數據
從1遍歷到10:
var:定義變量
begin:從哪開始
end:到哪結束
step:迭代步長(相當于x+=step)
計算從1加到10的和:
定義變量sum,值為0
通過foreach循環1到10,每循環一次加到另一個變量中
在循環中間定義變量,每次計算到該變量中
1
2
3
4
5
|
<c:set var= "sum" value= "0" scope= "page" ></c:set> <c:foreach begin= "1" end= "10" step= "1" var= "i" > <c:set var= "sum" value= "${sum + i}" scope= "page" ></c:set> </c:foreach> ${sum } |
varstatus屬性:
記錄迭代相關的信息:
屬性 | 類型 | 意義 |
index | number | 現在指到成員的索引 |
count | number | 總共指到成員的總數 |
first | boolean | 現在指到的成員是否是第一個成員 |
last | boolean | 現在指到的成員是否是最后一個成員 |
遍歷10到100的偶數,每到第三個數,顯示紅色:
1
2
3
4
5
6
7
8
|
<c:foreach begin= "10" end= "100" step= "2" var= "i" varstatus= "status" > <c: if test= "${status.count % 3 == 0}" > <font color= "red" >${i }</font> </c: if > <c: if test= "${status.count % 3 != 0}" > <font color= "blue" >${i }</font> </c: if > </c:foreach> |
在webroot/jstl目錄下新建foreach.jsp
實例代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
<% @page import = "cn.itcast.vo.user" %> <% @page import = "java.util.hashmap" %> <% @page import = "java.util.map" %> <% @page import = "java.util.arraylist" %> <% @page import = "java.util.list" %> <%@ 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> <h4>遍歷數組</h4> <% string [] arrs = { "美美" , "小鳳" , "芙蓉" , "小蒼" }; request.setattribute( "arrs" , arrs); %> <!-- for (string s : arrs){ } --> <c:foreach var= "s" items= "${ arrs }" > ${ s } </c:foreach> <h4>遍歷集合</h4> <% list<string> list = new arraylist<string>(); list.add( "美美" ); list.add( "小鳳" ); list.add( "芙蓉" ); list.add( "小澤" ); request.setattribute( "list" , list); %> <c:foreach var= "s" items= "${ list }" > ${ s } </c:foreach> <h4>遍歷map集合</h4> <% map<string,string> map = new hashmap<string,string>(); map.put( "aa" , "美美" ); map.put( "bb" , "小鳳" ); map.put( "cc" , "芙蓉" ); request.setattribute( "map" , map); %> <c:foreach var= "entry" items= "${ map }" > ${ entry.key } -- ${ entry.value } </c:foreach> <h4>遍歷對象的集合</h4> <% list<user> ulist = new arraylist<user>(); ulist.add( new user( "美美" , "123" )); ulist.add( new user( "小風" , "234" )); ulist.add( new user( "芙蓉" , "345" )); request.setattribute( "ulist" , ulist); %> <c:foreach var= "user" items= "${ ulist }" > ${ user.username } -- ${ user.password } </c:foreach> <h4>迭代數據</h4> <h4>迭代從 1 到 10 </h4> <c:foreach var= "i" begin= "1" end= "10" step= "2" > ${ i } </c:foreach> <h4>計算從 1 加到 100 的和</h4> <c:set var= "sum" value= "0" scope= "page" ></c:set> <c:foreach var= "i" begin= "1" end= "100" step= "1" varstatus= "status" > <c:set var= "sum" value= "${ sum + i }" ></c:set> </c:foreach> ${ sum } <h4>遍歷 10 到 100 的偶數,每到第 3 個數,顯示紅色</h4> <c:foreach var= "i" begin= "10" end= "100" step= "2" varstatus= "status" > <c:choose> <c:when test= "${ status.first }" > <font color= "blue" >${ i }</font> </c:when> <c:when test= "${ status.count % 3 eq 0 }" > <font color= "red" >${ i }</font> </c:when> <c:otherwise> ${ i } </c:otherwise> </c:choose> </c:foreach> </body> </html> |
<c:fortokens>
用來切分字符串
名稱 | 說明 | el | 類型 | 必須 | 默認值 |
var | 用來存放現在指到的成員 | n | string | 否 | 無 |
items | 被迭代的字符串 | y | string | 是 | 無 |
delims | 定義用來分割字符串的字符 | n | string | 是 | 無 |
varstatus | 用來存放現在指到的相關成員信息 | n | string | 否 | 無 |
begin | 開始的位置 | y | int | 否 | 0 |
end | 結束的位置 | y | int | 否 | 最后一個 |
1
2
3
4
|
<c:set var= "s" value= "aaa,bbb,ccc" scope= "page" ></c:set> <c:fortokens items= "${s}" delims= "," var= "e" > ${e } </c:fortokens> |
語法格式:
1
2
3
4
5
6
7
8
9
10
|
<c:fortokens items=要切分的字符串 delims=按著什么格式切分 var=定義變量 [varstatus= "varstatusname" ] [begin= "begin" ] [end= "end" ] [step= "step" ]> //body內容 </c:fortokens> |
在webroot/jstl目錄下新建fortokens.jsp
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<%@ 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> <h4>分隔字符串</h4> <c:set var= "i" value= "aa,bb,cc" scope= "page" ></c:set> <c:fortokens items= "${i }" delims= "," var= "x" > ${ x } </c:fortokens> </body> </html> |
<c:param>標簽
- 在jsp頁面進行url的相關操作時,經常要在url地址后面附加一些參數。<c:param>標簽可以嵌套在<c:import>、<c:url>或<c:redirect>標簽內,為這些標簽所使用的url地址附加參數。
- <c:param>標簽在為一個url地址附加參數時,將自動對參數值進行url編碼,例如:如果傳的參數值為“中國”,則將其轉換為“%d6%d0%b9%fa”后再附加到url地址后面,這也就是使用<c:param>標簽的最大好處
- 示例:<c:param name="name" value="value" />
<c:import>標簽
名稱 | 說明 | el | 類型 | 必須 | 默認值 |
url | 一文件被包含的地址 | y | string | 是 | 無 |
context | 項目虛擬路徑 | y | string | 否 | 無 |
var | 儲存被包含的文件的內容(以string類型存入) | y | string | 否 | 無 |
scope | var變量的jsp范圍 | n | string | 否 | page |
charencoding | 被包含文件的內容的編碼方式 | y | string | 否 | 無 |
varreader | 儲存被包含的文件的內容(以reader類型存入) | n | string | 否 |
1
2
3
4
5
6
7
8
9
|
<!-- 引入foreach.jsp 效果類似包含--> <c: import url= "/jstl/foreach.jsp" context= "/day8" ></c: import > <hr/> <!-- 引入不顯示,將內容保存另一個變量中 --> <c: import url= "/jstl/foreach.jsp" context= "/day8" var= "content" scope= "page" ></c: import > ${content } <c: import url= "http://java.sun.com" > <c:param name= "test" value= "1234" /> </c: import > |
在webroot/jstl/下新建 import.jsp(包含choose.jsp 并在choose.jsp 中獲取參數)
實例代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!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> <h4>jstl方式</h4> <c: import url= "/jstl/choose.jsp" context= "/day13" var= "i" scope= "page" > <c:param name= "username" value= "meimei" ></c:param> </c: import > ${ i } </body> </html> |
<c:url>標簽
<c:url>標簽用于在jsp頁面中構造一個url地址,其主要目的是實現url重寫。url重寫就是將會話標識以參數形式附加在url地址后面。(類似于session追蹤 尤其是當瀏覽器禁用cookie后,就是說實現了session追蹤的功能)
屬性名 | 是否支持el | 屬性類型 | 屬性描述 |
value | true | string | 指定要構造的url |
var | false | string | 指定將構造出的url結果保存到web域中的屬性名稱 |
scope | false | string | 指定將構造出的url結果保存在哪個域中 |
在webroot/jstl/下新建url.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<%@ 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> <h4>jstl方式</h4> <c:url var= "i" value= "/jstl/choose.jsp" scope= "request" context= "/day13" > <c:param name= "username" value= "xiaofeng" ></c:param> </c:url> <c:set var= "s" value= "劉勛" scope= "session" ></c:set> <a href= "${ i }" >choose</a> <br> i= ${i } <br> <% string url = "/day12/index.jsp" ; url = response.encodeurl(url); %> <!-- 將/day8/index.jsp 進行url重寫,保存page范圍 myurl中 --> <c:url value= "/index.jsp" context= "/day13" var= "myurl" scope= "page" /> url= <%=url %> <br> myurl= ${myurl } <br> <!-- 通過c:url 結合 c:param 對中文完成url編碼 --> <c:url value= "/login" context= "/day13" var= "myurl2" scope= "page" > <c:param name= "username" value= "張三" ></c:param> </c:url> myurl2= ${myurl2 } <br> </body> </html> |
修改choose.jsp 如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<%@ 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:out value= "${s }" ></c:out> <h4>獲取參數</h4> <%= request.getparameter( "username" ) %> <h4>傳統方式</h4> <% int a = 10 ; if (a >= 10 ){ out.print( "a >= 10" ); } else if (a < 10 ){ out.print( "a < 10" ); } else { out.print( "其他" ); } %> <h4>jstl方式</h4> <c:set var= "i" value= "10" scope= "page" ></c:set> <c:choose> <c:when test= "${ i ge 10 }" > i >= 10 </c:when> <c:when test= "${ i lt 10 }" > i < 10 </c:when> <c:otherwise> 其他 </c:otherwise> </c:choose> </body> </html> |
禁用瀏覽器的cookie后,運行如下:
點擊choose跳轉到choose.jsp
<c:redirect>標簽
<c:redirect>標簽用于實現請求重定向
屬性名 | 是否支持el | 屬性類型 | 屬性描述 |
url | true | string | 指定要轉發或重定向到的目標資源的url地址 |
context | true | string |
當要使用相對路徑重定向到同一個服務器下的其他web應用程序中的 資源時,context屬性指定其他web應用程序的名稱 |
注意:如果重定向第三方的網站時要使用絕對路徑(不能再使用context)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<% // 以前重定向 // response.sendredirect("/day12/index.jsp"); %> <c:redirect url= "/index.jsp" context= "/day12" ></c:redirect> 在webroot/jstl下新建redirect.jsp [html] view plain copy 在code上查看代碼片派生到我的代碼片 <%@ 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> <h4>jstl方式</h4> <c:redirect url= "/jstl/choose.jsp" context= "/day13" > <c:param name= "username" value= "furong" ></c:param> </c:redirect> </body> </html> |
el函數庫
jstl中的常用el函數
el函數是用來操作字符串的
由于在jsp頁面中顯示數據時,經常需要對顯示的字符串進行處理,sun公司針對一些常見處理定義了一套el函數庫供開發者使用。
這些el函數在jstl開發包中進行描述,因此在jsp頁面中使用sun公司的el函數庫,需要導入jstl開發包,并在頁面中導入el函數庫,如下所示:
在頁面中使用jstl定義的el函數:
1
2
|
<% @taglib uri= "http://java.sun.com/jsp/jstl/functions" prefix= "fn" %> (可以在fn.tld文件中查看) |
fn:tolowercase
fn:tolowercase函數將一個字符串中包含的所有字符轉換為小寫形式,并返回轉換后的字符串,它接收一個字符串類型的參數,例如:
fn:tolowercase("")的返回值為字符串""
fn.tolowercase("") 的返回值為空字符串
fn:touppercase
fn:touppercase函數將一個字符串中包含的所有字符轉換為大寫形式,并返回轉換后的字符串,它接收一個字符串類型的參數。例如:
fn:touppercase("")的返回值為字符串""
fn:touppercase("")的返回值為空字符串
fn:trim
fn:trim函數刪除一個字符串的首尾的空格,并返回刪除空格后的結果字符串,它接收一個字符串類型的參數。需要注意的是,
fn:trim函數不能刪除字符串中間位置的空格
例如:fn:trim(" 15.org ")的返回值字符串是" 315.org"
fn:length
fn:length函數返回一個集合或數組大小,或返回一個字符串中包含的字符的個數,返回值是int類型。fn:length函數接收一個參數,這個參數可以是<c:foreach>標簽的items屬性支持的任何類型,包括任意類型的數組、java.util.collection、java.util.iterator、java.util.enumeration、java.util.map等類的實例對象和字符串。
如果fn:length函數的參數為null或者是元素個數為0的集合或數組對象,則函數返回0;如果參數是空字符串,則函數返回0;
fn:split
fn:split函數以指定字符串作為分隔符,將一個字符串分割成數組并返回這個字符串數組。
fn:split函數接收兩個字符串類型的參數,第一個參數表示要分割的字符串,第二個參數表示作為分隔符的字符串。
例如:fn:split("]的返回值為字符串"it315"
fn:join
fn:join函數以一個字符串作為分隔符,將一個字符串數組中的所有元素合并為一個字符串并返回合并后的結果字符串。fn:join函數接收兩個參數,第一個參數是要操作的字符串數組,第二個參數是作為分隔符的字符串。
如果fn:join函數的第二個參數是空字符串,則fn:join函數的返回值直接將元素連接起來。例如:
假設stringarray是保存在web域中的一個屬性,它表示一個值為{"www","it315","org"}的字符串數組,則fn:join(stringarray,".")返
回字符串""
fn:join(fn:split("www,it315,org",","),".") 的返回值為字符串""
fn:indexof
fn:indexof 函數返回指定字符串在一個字符串中第一次出現的索引值,返回值為int類型。fn:indexof函數接收兩個字符串類型的參數,如果第一個參數字符串中包含第二個參數字符串,那么不管第二個參數字符串在第一個參數字符串中出現幾次,
fn:indexof函數總是返回第一次出現的索引值;如果第一個參數中不包含第二個參數,則fn:indexof函數返回-1。如果第二個參數為空字符串,則fn:indexof函數總是返回0。
例如:fn:indexof("") 的返回值為5
fn:contains
fn:contains函數檢測一個字符串中是否包含指定的字符串,返回值為布爾類型。fn:contains函數在比較兩個字符串是否相等時是大小寫敏感的。
fn:contains函數接收兩個字符串類型的參數,如果第一個字符串中包含第二個參數字符串返回true,否則返回false。如果第二個參數的值為空字符串,則fn:contains函數總是返回true。實際上,fn:contains(string,substring)等價于fn:indexof(string,sunstring)!= -1
忽略大小寫的el函數:fn:containsignorecase
fn:startswith
fn:startswith 函數用于檢測一個字符串是否以指定的字符串開始的,返回值為布爾類型。
fn:startswith 函數接收兩個字符串類型的參數,如果第一個參數字符串以第二個參數字符串開始,則函數返回true,否則函數返回false。如果第二個參數為空字符串,則fn:startswith函數總是返回true。例如:
fn:startswith("")的返回值為false
與之對應的el函數:fn:endswith
fn:replace
fn:replace函數將一個字符串中包含的指定字符串替換為其他的指定字符串,并返回替換后的結果字符串。fn:replace("www it315 org"," ",".")的返回值為字符串""
fn:substring
fn:substring 函數用于截取一個字符串的子字符串并返回截取到的子字符串。fn:substring函數接收三個參數,第一個參數是用于指定要操作的源字符串,第二個參數是用于指定截取子字符串開始的索引值,第三個參數是用于指定截取子字符串結束的索引值,第二個參數和第三個參數都是int類型,其值都從0開始例如:
fn:substring(")的返回值為字符串"it315"
fn:substringafter
fn:substringafter函數用于截取并返回一個字符串中的指定字符串第一次出現之后的子字符串。fn:substringafter函數接收兩個字符串類型的參數,第一個參數表示要操作的源字符串,第二個參數表示指定的子字符串。
fn:substringafter("",".")的返回值為字符串"it315.org"
與之對應的el函數為:fn:substringbefore
自定義el函數開發步驟
el自定義函數開發與應用包括以下三個步驟:
編寫一個java類,方法必須是靜態方法。
在web-inf目錄下新建一個tld的文件。
沒有標簽的提示,復制 , 合并名稱
設置2.0,設置url和shortname
編寫標簽庫描述符(tld)文件,在tld文件配置自定義函數
使用function標簽配置自定義函數。
使用name標簽配置方法名(可以任意)
使用function-class標簽配置類的全路徑
使用function-signature 標簽配置返回值類型(中間有空格)方法名稱(參數類型)
在jsp頁面中導入和自定義函數
開發el function注意事項
編寫標簽庫描述文件后,需要將它放置到<web應用>\web-inf目錄中或web-inf目錄下的除了classes和lib目錄之外的任意子目錄中。
tld文件中的<uri>元素用指定該tld文件的uri,在jsp文件中需要通過這個uri來引入該標簽庫描述文件。
<function>元素用于描述一個el自定義函數,其中:
<name>子元素用于指定el自定義函數的名稱。
<funtion-class>子元素用于指定完整的java類名。
<function-signature>子元素用于指定java類中的靜態方法的簽名,方法簽名必須指明方法的返回類型以及各個參數的類型,
各個參數之間用逗號分隔。
實例如下:
在src下新建一個cn.itcast.el的包,在包內新建eldemo1.java
代碼如下:
1
2
3
4
5
6
|
package cn.itcast.el; public class eldemo1 { public static string sayhello(string name){ return "hello " +name; } } |
在webroot/web-inf下新建myfn的tld文件 并進行配置:
配置后的代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?xml version= "1.0" encoding= "utf-8" ?> <taglib version= "2.0" xmlns= "http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" > <tlib-version> 1.0 </tlib-version> < short -name>myfn</ short -name> <uri>http: //www.itcast.cn/1110/myfn</uri> <!-- 配置自定義的el函數 --> <function> <!-- 配置方法名稱 --> <name>sayhi</name> <!-- 方法所在的類 --> <function- class >cn.itcast.el.eldemo1</function- class > <!-- 配置方法的簽名 --> <function-signature>java.lang.string sayhello(java.lang.string)</function-signature> </function> </taglib> |
在webroot根目錄下新建el文件夾,在里面新建demo.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<%@ page language= "java" contenttype= "text/html; charset=utf-8" pageencoding= "utf-8" %> <%@ taglib uri= "http://java.sun.com/jsp/jstl/functions" prefix= "fn" %> <%@ taglib uri= "http://www.itcast.cn/1110/myfn" prefix= "myfn" %> <!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> ${ fn:length( "abcdefg" ) } ${ fn:touppercase( "abcdefg" ) } ${ myfn:sayhi( "小風" ) } </body> </html> |
自定義標簽
自定義標簽簡介
自定義標簽主要用于移除jsp頁面中的java代碼,提高代碼的復用性
使用自定義標簽移除jsp頁面在奇偶那個的java代碼,只需要完成以下步驟:
- 編寫一個實現tag接口的java類(標簽處理器)
- 編寫標簽庫描述符(tld)文件,在tld文件中對標簽處理器類描述成一個標簽
- 參考tomcat中example項目中的jsp部分
因為企業業務需求是多種多樣的,所以常見開源框架只能提供通用的java代碼功能,如果實現既定業務邏輯功能,需要自定義標簽。通過自定義標簽(簡化復雜業務開發)
簡單標簽
由于傳統標簽使用三個標簽接口來完成不同的功能,顯得過于繁瑣,不利于標簽技術的推廣,sun公司為降低標簽技術的學習難度,在jsp2.0中定義了一個更為簡單、便于編寫和調用的simpletag接口來實現標簽的功能。實現simpletag接口的標簽通常稱為簡單標簽。
simpletag方法介紹
- setjspcontext方法:用于把jsp頁面的pagecontext對象傳遞給標簽處理器對象。
- setparent方法:用于把父標簽處理器對象傳遞給當前標簽處理器對象。
- getparent方法:用于獲得當前標簽的父標簽處理對象。
- setjspbody方法:用于把代表標簽體的jspfragment對象傳遞給標簽處理器對象。即傳入標簽體緩存對象(封裝了標簽體內容)
- dotag方法:用于完后所有標簽邏輯,包括輸出、迭代、修改標簽體內容等。在dotag方法中可以拋出
- javax.servlet.jsp.skippageexception異常,用于通知web容器不再執行jsp頁面中位于結束標記后面的內容,這等效于在傳統標簽的doendtag方法中返回tag.skip_page常量的情況。
簡單標簽庫開發常用功能,實現simpletag接口標簽類(simpletag jsp2.0 之后為了簡化標簽開發提供的)
編寫簡單的標簽類,只需要繼承simpletagsupport類
setjspcontext 傳入pagecontext對象
setparent 傳入父標簽對象
setjspbody 傳入標簽體緩存對象(封裝了標簽體內容)
dotag (執行標簽,在該方法中編寫標簽代碼邏輯)
在setjspbody方法中傳入對象jspfragment,該對象封裝了標簽體內容,控制標簽體內容輸出
最重要方法invoke(writer out) 意義將標簽體內容輸出到指定字符輸出流中。
注意:在簡單標簽庫中<bodycontent>不能寫jsp,而必須寫scriptless
simpletag接口方法的執行順序:
當web容器開始執行標簽時,會調用如下方法完成標簽的初始化:
- web容器調用標簽處理器對象的setjspcontext方法,將代表jsp頁面的pagecontext對象傳遞給標簽處理器對象。
- web容器調用標簽處理器對象的setparent方法,將父標簽處理器對象傳遞給這個標簽處理器對象。注意,只有在標簽存在父標簽的情況下,web容器才會調用這個方法。
- 如果調用標簽時設置了屬性,容器將調用每個屬性對應的setter方法把屬性值傳遞給標簽處理器對象。如果標簽的屬性值是el表達式,則web容器首先計算表達式的值,然后把值傳遞給標簽處理器對象。
- 如果簡單標簽有標簽體,容器將調用setjspbody方法把代表標簽體的jspfragment對象傳遞進來。
執行標簽時:
- 容器調用標簽處理器的dotag() 方法,開發人員在方法內通過操作jspfragment對象,就可以實現是否執行、迭代、修改標簽體的目的。
jspfragment類
(1)javax.servlet.jsp.tagext.jspfragment類是在jsp2.0中定義的,它的實例對象代表jsp頁面中的一段符合jsp語法規范的jsp片段,這段jsp片段中不能包含jsp腳本元素。
(2)web容器在處理簡單標簽的標簽體時,會把標簽體內容用一個jspfragment對象表示,并調用標簽處理器對象的setjspbody方法把jspfragment對象傳遞給標簽處理器對象。jspfragment類中只定義了兩個方法,如下所示:
getjspcontext方法:用于返回代表調用頁面的jspcontext對象——pagecontext
public abstract void invoke(java.io.writer out) 輸出標簽內容
用于執行jspfragment對象所代表的jsp代碼片段
參數out用于指定將jspfragment對象的執行結果寫入到哪個輸出流對象中,如果傳遞給參數out的值為null,則將執行結果寫入到jspcontext.getout()方法返回的輸出流對象中。(簡而言之,可以理解為寫給瀏覽器)
invoke方法詳解
jspfragment.invoke方法是jspfragment最重要的方法,利用這個方法可以控制是否執行和輸出標簽體的內容、是否迭代執行標簽體的內容或對標簽體的執行結果進行修改后再輸出。例如:
- 在標簽處理器中如果沒有調用jspfragment.invoke方法,其結果就相當于忽略標簽體內容;
- 在標簽體處理器中重復調用jspfragment.invoke方法,則標簽體內容會被重復執行;
- 若想在標簽處理器中修改標簽體內容,只需在調用invoke方法時指定一個可取出結果數據的輸出流對象(例如:stringwriter),讓標簽體的執行結果輸出到該輸出流對象中,然后從該輸出流對象中取出數據進行修改后在輸出到目標設備,即可達到修改標簽體的目的。
例:控制標簽后的jsp頁面是否執行
dotag
throw new skippageexception
tld配置
1
2
3
4
5
|
<tag> <name>demo2</name> <tag- class >simple.mytag2</tag- class > <body-content>empty</body-content> </tag> |
自定義標簽入門
需求:自定義標簽<myc:print>在頁面中輸出hello
步驟一:編寫標簽類
編寫一個類,繼承simpletagsupport
重寫兩個方法,dotag()和setjspcontext(jspcontext pc)
通過jspcontext 對象可以獲取out對象向外輸出內容
步驟二:提供tld文件進行配置
通過<tag>標簽配置自定義標簽
配置標簽名稱<name>print</name>
配置標簽的類<tag-class>xxx.xxx.xxx</tag-class>
配置標簽體的內容<body-content>empty</body-content>
步驟三:在jsp頁面中引入該標簽庫
自定義帶有標簽體的標簽
需求:自定義標簽<myc:out>在頁面中輸出內容
步驟一:編寫標簽類
編寫一個類,繼承simpletagsupport
重寫兩個方法,dotag()和setjspcontext(jspcontext pc)
在dotag()中通過getjspbody()獲取jspfragment標簽體,調用invoke(null)
步驟二:提供tld文件進行配置
通過<tag>標簽配置自定義標簽
配置標簽名稱<name>out</name>
配置標簽的類<tag-class>xxx.xxx.xxx</tag-class>
配置標簽體的內容<body-content>scriptless</body-content>
步驟三:在jsp的頁面中引入該標簽庫
body-content的配置
<body-content>元素的可選值有:
- empty:不能有標簽體內容
- jsp:標簽體內容可以是任何東西:el、jstl、<%= %>、<%%>,以及html;但不建議使用java代碼段,simpletag已經不再支持使用<body-content>jsp</body-content>
- scriptless:標簽體內容不能是java代碼段,但可以是el、jstl等。
- tagdependent:標簽體內容不做運算,由標簽處理類自行處理,無論標簽體內容是el、jsp、jstl,都不會做運算。
自定義帶有屬性的標簽
需求:自定義標簽<myc:if>在頁面中輸出內容
步驟一:編寫標簽類
編寫一個類,繼承simpletagsupport類。
提供頁面的屬性,設置成boolean類型。并提供set方法,頁面的屬性與類中的屬性名稱必須相同。
重寫dotag()方法,判斷屬性是否為true,通過getjspbody()獲取jspfragment標簽體,調用invoke(null)
步驟二:提供tld文件進行配置
通過<tag>標簽配置自定義標簽
配置標簽名稱<name>if</name>
配置標簽的類<tag-class>xxx.xxx.xxx</tag-class>
配置標簽體的內容<body-content>scriptless</body-content>
配置屬性<attribute>在屬性中配置其他信息</attribute>
步驟三:在jsp頁面中引入標簽庫
attribute的配置
(1)配置屬性名稱
<name>test</name>
(2)屬性是否是必須的
<required>true</required>
(3)配置是否支持el表達式
<rtexprvalue>true</rtexprvalue>
(4)配置屬性的類型
<type>boolean</type>
在tld中描述標簽屬性attribute
元素名 | 是否必須指定 | 描述 |
description | 否 | 用于指定屬性的描述信息 |
name | 是 |
用于指定屬性的名稱。屬性名稱是大小寫敏感的,并且不能以jsp、 _jsp、java和sun開頭 |
required | 否 |
用于指定在jsp頁面中調用自定義標簽時是否必須設置這個屬性。其 取值包括true和false,默認值是false,true表示必須設置,否則可以 設置也可以不設置該屬性。 |
rtexprvalue | 否 |
rtexprvalue是runtime expression value(運行時表達式)的英文簡寫, 用于指定屬性值是一個靜態值或動態值。其取值包括true和false,默認值 是false,false表示只能為該屬性指定靜態文本值,例如"123"; true表示可 以為該屬性指定一個jsp動態元素,動態元素的結果作為屬性值,例如 jsp表達式<%=value %> |
type | 否 | 用于指定屬性值的java類型。默認是string |
<tag>元素的<attribute>子元素用于描述自定義
標簽的一個屬性,自定義標簽所具有的每個屬性
都要對應一個<attribute>元素 。
1
2
3
4
5
6
7
|
<attribute> <description>description</description> <name>aaaa</name> <required> true </required> <rtexprvalue> true </rtexprvalue> <type>objecttype</type> </attribute> |
實例如下:
在web項目的src目錄下新建cn.itcast.tag包,在包內新建三個標簽實現類
tagdemo1.java (沒有標簽體的自定義標簽)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package cn.itcast.tag; import java.io.ioexception; import javax.servlet.jsp.jspcontext; import javax.servlet.jsp.jspexception; import javax.servlet.jsp.pagecontext; import javax.servlet.jsp.tagext.simpletagsupport; /** * 對外輸出hello * @author administrator * */ public class tagdemo1 extends simpletagsupport{ private pagecontext pc; public void dotag() throws jspexception, ioexception { pc.getout().write( "hello" ); } /** * 服務器默認先執行該方法 */ public void setjspcontext(jspcontext pc) { this .pc = (pagecontext) pc; } } |
tagdemo2.java (有標簽體 處理標簽體內容)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package cn.itcast.tag; import java.io.ioexception; import java.io.stringwriter; import javax.servlet.jsp.jspcontext; import javax.servlet.jsp.jspexception; import javax.servlet.jsp.pagecontext; import javax.servlet.jsp.tagext.jspfragment; import javax.servlet.jsp.tagext.simpletagsupport; /** * 帶有標簽主體 * @author administrator * */ public class tagdemo2 extends simpletagsupport{ private pagecontext pc; public void dotag() throws jspexception, ioexception { jspfragment jf = getjspbody(); stringwriter sw = new stringwriter(); //通過invoke方法將標簽體內容寫入到參數writer對象sw中 jf.invoke(sw); // 獲取標簽體內容 string content = sw.tostring().touppercase(); pc.getout().print(content); } public void setjspcontext(jspcontext pc) { this .pc = (pagecontext)pc; } } |
tagdemo3.java (有屬性 有標簽體的自定義標簽)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package cn.itcast.tag; import java.io.ioexception; import javax.servlet.jsp.jspexception; import javax.servlet.jsp.tagext.simpletagsupport; /** * 類似<c:if>標簽,帶有屬性的 * @author administrator * */ public class tagdemo3 extends simpletagsupport{ private boolean test; public void settest( boolean test) { this .test = test; } public void dotag() throws jspexception, ioexception { if (test){ getjspbody().invoke( null ); } } } |
在webroot/web-inf 目錄下新建myc.tld文件
配置內容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
<?xml version= "1.0" encoding= "utf-8" ?> <taglib version= "2.0" xmlns= "http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" > <tlib-version> 1.0 </tlib-version> < short -name>myc</ short -name> <uri>http: //www.itcast.cn/1110/myc</uri> <!-- 配置自定義標簽 --> <tag> <!-- 配置標簽名稱 --> <name>print</name> <!-- 配置標簽的類 --> <tag- class >cn.itcast.tag.tagdemo1</tag- class > <!-- 配置標簽主體 --> <body-content>empty</body-content> </tag> <!-- 配置自定義標簽 --> <tag> <!-- 配置標簽名稱 --> <name>out</name> <!-- 配置標簽的類 --> <tag- class >cn.itcast.tag.tagdemo2</tag- class > <!-- 配置標簽主體 --> <body-content>scriptless</body-content> </tag> <!-- 配置自定義標簽 --> <tag> <!-- 配置標簽名稱 --> <name> if </name> <!-- 配置標簽的類 --> <tag- class >cn.itcast.tag.tagdemo3</tag- class > <!-- 配置標簽主體 --> <body-content>scriptless</body-content> <!-- 配置屬性 --> <attribute> <!-- 配置屬性名稱 --> <name>test</name> <!-- 屬性是否是必須的 --> <required> true </required> <!-- 是否支持el表達式 --> <rtexprvalue> true </rtexprvalue> <!-- 屬性的類型 --> <type> boolean </type> </attribute> </tag> </taglib> |
在webroot下新建tag文件夾,新建tag.jsp 測試自定義標簽內容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<%@ page language= "java" contenttype= "text/html; charset=utf-8" pageencoding= "utf-8" %> <%@ taglib uri= "http://www.itcast.cn/1110/myc" prefix= "myc" %> <%@ 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> <myc:print/> <myc:out> liuxun1993 </myc:out> <c:set var= "i" value= "10" ></c:set> <myc: if test= "${ i eq 10 }" > 美美 </myc: if > </body> </html> |
啟動服務器,運行結果如下:
以上所述是小編給大家介紹的javaweb開發之jstl標簽庫的使用、 自定義el函數、自定義標簽(帶屬性的、帶標簽體的),希望對大家有所幫助