JSTL 簡介
JavaServer Pages Standard Tag Library由JCP(Java Community Process)指定標準
提供給 Java Web 開發(fā)人員一個標準通用的標簽函數(shù)庫
和 EL 配合來取代傳統(tǒng)直接在頁面上嵌入 Java 程序(Scripting)的做法,以提高程序可讀性、維護性和方便性
JSTL1.1安裝
下載
JSTL 主要由Apache組織的Jakarta Project 實現(xiàn)
http://tomcat.apache.org/taglibs/standard/
容器必須支持Servlet 2.4 且JSP 2.0 以上版本
JavaEE1.4
安裝
解壓縮后將lib 中的jstl.jar、standard.jar 復制到WEB應用程序的WEB-INF\lib 下
1.核心標簽庫(core)
<c:out> 標簽用于輸出一段文本內容到pageContext對象當前保存的“out”對象中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | < h1 >HTML轉義輸出</ h1 >< hr > < a href = "#" >xxx</ a > < c:out value="<a href = '#' >xxx</ a >" ></ c:out > ${fn:escapeXml('< a href = "#" >xxx</ a >') } < h1 >輸出默認值</ h1 >< hr > <% String addr = "西二旗"; //pageContext.setAttribute("addr",addr); %> < c:out value = "${addr}" default = "北京" ></ c:out > ${addr == null?"北京" : addr } < h1 >輸出變量</ h1 >< hr > <% String name = "無雙"; pageContext.setAttribute("name",name); %> < c:out value = "${name}" ></ c:out > ${name } < h1 >輸出常量</ h1 >< hr > < c:out value = "阿斯蒂芬" ></ c:out > ${"啦啦啦啦" } |
<c:set>標簽用于設置、修改域中的屬性和值,默認的域是page
或者設置、修改Web域中的Map的鍵和值,或JavaBean的屬性值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | < h1 >修改域中的JavaBean的屬性的值</ h1 >< hr > <% Person p = new Person(); pageContext.setAttribute("p",p); %> < c:set target = "${p}" property = "name" value = "克林頓" ></ c:set > ${p.name } < h1 >設置或修改域中的Map的值</ h1 >< hr > <% Map map = new HashMap(); pageContext.setAttribute("map",map); %> < c:set target = "${map}" property = "cellphone" value = "10010" ></ c:set > < c:set target = "${map}" property = "cellphone" value = "10086" ></ c:set > ${map.cellphone } < h1 >設置或修改域中的屬性值</ h1 >< hr > < c:set var = "name" value = "韋小寶" ></ c:set > < c:set var = "name" value = "阿珂" ></ c:set > |
<c:remove>標簽用于刪除各種Web域中的屬性,如果不寫域名,則刪除4個域中所有的同名的屬性。
1 | < c:remove var = "name" scope=”request”/> |
<c:catch>標簽用于捕獲嵌套在<c:catch>標簽體中的內容,拋出的異常,
其語法格式如下:<c:catch var="e" >nested actions</c:catch> 將捕獲的異常對象以e為的名字,存放到page域中,再利用el表達式, ${e.message} ,頁面就會顯示出異常信息。
1 2 3 4 5 6 | < c:catch var = "e" > <% int i = 1/0; %> </ c:catch > ${e.message } |
*<c:if test=“$(2>1)”>標簽可以構造簡單的“if-then”結構的條件表達式
test后接一個el表達式,el返回值為boolean類型,若為true,則執(zhí)行標簽體中的內容。想達到if-else的目的,只能在寫一次c:if,將條件取反。
1 2 3 4 5 6 | < c:if test="${2>1}"> 確實是這樣的.... </ c:if > < c:if test = "${2<=1}" > 你確定嗎? </ c:if > |
*<c:choose>標簽用于指定多個條件選擇的組合邊界,它必須與<c:when>和<c:otherwise>標簽一起使用。使用<c:choose>,<c:when>和<c:otherwise>三個標簽,可以構造類似 “if-else if-else” 的復雜條件判斷結構。
1 2 3 4 5 6 7 | < c:choose > < c:when test = "${count == 0}" > 對不起,沒有符合您要求的記錄。 </ c:when > < c:otherwise > 符合您要求的記錄共有${count}條. </ c:otherwise > |
*<c:forEach>標簽用于對一個集合對象中的元素進行循環(huán)迭代操作,或者按指定的次數(shù)重復迭代執(zhí)行標簽體中的內容。
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 | < h1 >實驗:遍歷10到100的偶數(shù),如果數(shù)字所在的位置是3的倍數(shù),顯示成紅色</ h1 >< hr > < c:forEach begin = "10" end = "100" step = "2" var = "i" varStatus = "stat" > < c:if test = "${stat.count % 3 == 0}" > //count表示當前數(shù)字的總數(shù) index自然數(shù)的位置 < font color = "red" > ${i } </ font > </ c:if > < c:if test = "${stat.count % 3 != 0}" > < font color = "blue" > ${i } </ font > </ c:if > </ c:forEach > < h1 >循環(huán)執(zhí)行指定的內容若干次</ h1 >< hr > < c:forEach begin = "0" end = "10" step = "2" var = "i" > ${i }, //0,2,4,6,8,10, </ c:forEach > < h1 >遍歷Map中的數(shù)據(jù)</ h1 >< hr > <% Map map = new LinkedHashMap(); map.put("name","曹操"); map.put("age","59"); map.put("wife","小喬"); map.put("gender","男"); pageContext.setAttribute("map",map); %> < c:forEach items = "${map}" var = "entry" > //entry存放的是一對鍵值 ${entry.key } : ${entry.value }< br > </ c:forEach > < h1 >遍歷集合中的數(shù)據(jù)</ h1 >< hr > <% List list = new ArrayList(); list.add("美國"); list.add("中國"); list.add("俄羅斯"); list.add("印度"); list.add("巴西"); pageContext.setAttribute("list",list); %> < c:forEach items = "${list}" var = "c" > ${c }< br > </ c:forEach > < h1 >遍歷數(shù)組中的數(shù)據(jù)</ h1 >< hr > <% String city = {"北京","上海","廣州","鐵嶺","葫蘆島"}; pageContext.setAttribute("city",city); %> < c:forEach items = "${city}" var = "c" > ${c }< br > </ c:forEach > |
*<c:forTokens>切割字符串,將切好的字符串對象存在數(shù)組了,遍歷數(shù)組的字符串對象。
1 2 3 | < c:forTokens items = "www.itheima.com" delims = "." var = "str" > ${str }< br > //str表示數(shù)組中的一個字符串對象 </ c:forTokens > //wwwitheimacom |
<c:import> 標簽,實現(xiàn)include操作
1 2 | < c:import url = "/index.jsp" var = "p" scope = "page" ></ c:import > //將index.jsp內容,作為對象p 存到當前頁面的page域中,方便引用。 |
<c:url>標簽用于在JSP頁面中構造一個URL地址,其主要目的是實現(xiàn)URL重寫。URL重寫就是將會話標識號以參數(shù)形式附加在URL地址后面
1 2 3 4 5 6 | <% String url = response.encodeURL(request.getContextPath()+"/index.jsp"); %> < a href="<%= url %>">hhhh</ a > < c:url value = "/index.jsp" context = "${pageContext.request.contextPath}" var = "url" scope = "page" ></ c:url > < a href = "${url }" >xxx</ a > |
<c:redirect>標簽用于實現(xiàn)請求重定向
1 2 3 | < c:redirect url = "/index.jsp" context = "${pageContext.request.contextPath}" > < c:param name = "name" value = "zhang" ></ c:param > </ c:redirect > |
<c:param>標簽 在JSP頁面進行URL的相關操作時,經(jīng)常要在URL地址后面附加一些參數(shù)。<c:param>標簽可以嵌套在<c:import>、<c:url>或<c:redirect>標簽內,為這些標簽所使用的URL地址附加參數(shù)。
傳統(tǒng)標簽:
(1)寫一個類實現(xiàn)Tag接口 javax.servlet.jsp.tagext.Tag
(2)寫一個tld文件,描述寫好的類
tld文件放在WEB-INF文件夾下(除classes和lib以外)。tld文件的schemaLocation不對,要修改為,也就是把前面的地址復制一下粘貼到后面的地址再加個/
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
(3)在jsp頁面中引入tld文件,就可以在jsp頁面中使用自定義標簽了
生命周期:
第一次自定義標簽被訪問的時候產生對象,駐留在內存中,隨著web應用的銷毀而銷毀。
執(zhí)行過程:
setPageContext getParent dastart doend releast
特點:
分為doStartTag 和 doEndTag方法來分別處理發(fā)現(xiàn)開始標簽和發(fā)現(xiàn)結束標簽時的代碼,在doStartTag可以通過返回值來控制標簽體是否允許執(zhí)行,在doEndTag方法里可以通過返回值控制標簽之后的剩余頁面是否允許執(zhí)行
傳統(tǒng)標簽的這種開發(fā)方式,需要我們分析發(fā)現(xiàn)開始標簽和發(fā)現(xiàn)結束標簽時都需要執(zhí)行什么代碼,還需要分析到底要返回什么樣的標簽體控制程序執(zhí)行,相對來說相當?shù)姆爆?/span>。
簡單標簽:
(1)寫一個類實現(xiàn)SimpleTag接口(繼承SimpleTag接口的默認實現(xiàn)類SimpleTagSupport)
(2)寫一個tld文件,描述寫好的類
(3)在jsp頁面中引入tld文件,就可以在jsp頁面中使用自定義標簽了
生命周期+執(zhí)行過程
1.當jsp在執(zhí)行的過程中,每當遇到一個簡單標簽時,都會創(chuàng)建一個處理類對象.
2.調用setJspContext傳入當前jsp頁面的PageContext對象.
3.如果當前標簽有父標簽則調用setParent方法將父標簽傳入,如果沒有父標簽則這個方法不會被調用.
4.如果該標簽具有屬性,調用屬性的setXXX方法將屬性的值傳入
5.如果當前標簽具有標簽體,則會調用setJspBody將封裝了標簽體信息的JspFragment傳入,如果沒有標簽體,這個方法不執(zhí)行
6.最后調用doTag方法,在這個方法里我們可以書寫處理標簽事件的java代碼
7.當自定義標簽執(zhí)行完成后,簡單標簽對象就銷毀掉了.
要實現(xiàn)的功能:
控制標簽體是否執(zhí)行<c:if>
控制標簽之后的內容是否執(zhí)行
控制標簽體重復執(zhí)行<c:foreach>
修改標簽體后輸出<c:out>
為自定義標簽來增加一個屬性:
在標簽處理類中增加一個javabean屬性,這個屬性就是要增加的標簽的屬性,并對外提供setXXX方法。
在tld文件中這個標簽的描述中描述一下該屬性
*想要開發(fā)一個簡單標簽,
寫一個類繼承SimpleTagSupport覆蓋doTag方法就可以了,可以調用getJspContext/getJspBody來獲取需要的內容
*在tld文件中對標簽進行描述
<tlib-version>1.0</tlib-version>
<short-name>MyTag</short-name> //標識tld文件
<uri>http://www.itheima.com/MyTag</uri>
<tag>
<name>simpleDemo1</name> //標簽的名字
<tag-class>com.itheima.simletag.SimpleDemo1</tag-class> //標簽的處理類
<body-content>scriptless</body-content>
//標簽體的類型 JSP(簡單標簽不能寫) Scriptless(任意的jsp內容,不包括java代碼) empty(空標簽) tagdependent(標簽體是給后臺用的,一般不用這種類型)
<attribute> //聲明一個屬性,可以聲明多個屬性
<name>times</name> //屬性的名字
<required>true</required> //是否為必須存在的屬性
<rtexprvalue>true</rtexprvalue> //是否支持el表達式
<type>int</type> // 屬性的java類型
</attribute>
</tag>
自定義標簽開發(fā)_案例
自定義標簽打jar包
1、新建一個java工程,Exec
2、將繼承SimpleTagSupport的java類復制到src目錄下
3、工程名 - 右鍵 - build path - configure build path - libraries - add library
- myeclipes library - javaEE 5 libraries
4. 工程名 右鍵新建文件夾META-INF 文件夾
5. 將tld文件放入WEB-INF文件夾下
6. 工程名 右鍵-Export - jar - JAR file - 勾選要打包的工程 - 選好保存的路徑
IFTag.java
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class IFTag extends SimpleTagSupport { private boolean test; public void setTest( boolean test) { this .test = test; } @Override public void doTag() throws JspException, IOException { if (test){ getJspBody().invoke( null ); } } } |
Exec.tld
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | < tlib-version >1.0</ tlib-version > < short-name >exec</ short-name > < uri >http://www.itheima.com/exec</ uri > < tag > < name >if</ name > < tag-class >com.itheima.exec.IFTag</ tag-class > < body-content >scriptless</ body-content > < attribute > < name >test</ name > < required >true</ required > < rtexprvalue >true</ rtexprvalue > < type >boolean</ type > </ attribute > </ tag > |
exec.jsp
1 2 3 4 5 6 7 | < exec:if test="${3>2}"> 確實是這樣的! </ exec:if > < exec:if test = "${3<=2}" > 你確定嗎? </ exec:if > 、 |