javaweb學習總結(二十七)——jsp簡單標籤開發案例和打包

一、開發標籤庫

1.1、開發防盜鏈標籤

  1、編寫標籤處理器類:RefererTag.java

複製代碼
 1 package me.gacl.web.simpletag;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.http.HttpServletRequest;
 5 import javax.servlet.http.HttpServletResponse;
 6 import javax.servlet.jsp.JspException;
 7 import javax.servlet.jsp.PageContext;
 8 import javax.servlet.jsp.SkipPageException;
 9 import javax.servlet.jsp.tagext.SimpleTagSupport;
10 
11 /**
12  * @author gacl
13  * 防盜鏈標籤RefererTag
14  */
15 public class RefererTag extends SimpleTagSupport {
16 
17     /**
18      * 網站域名
19      */
20     private String site;   //頁面上傳來的值:http://192.168.188.53:7001
21     
22     /**
23      * 要跳轉的頁面
24      */
25     private String page;   //頁面上傳來的值:/index.jsp 26     
27     @Override
28     public void doTag() throws JspException, IOException {
29         //獲取jsp頁面的PageContext對象
30         PageContext pageContext = (PageContext) this.getJspContext();
31         //通過PageContext對象來獲取HttpServletRequest對象
32         HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
33         //獲取請求的來路(Referer)
34         String referer = request.getHeader("referer"); //http://192.168.188.53.:7001/xmlDemo/index.jsp 不知道爲什麼53後面有一個.點
35         //如果來路是null或者來路不是來自我們自己的site,那麼就將請求重定向到page頁面
36         if (referer == null || !referer.startsWith(site)) { //原文中的代碼:沒有效果,一直頁面不變          
         if (referer == null || referer.startsWith(site)) { //修改後的代碼,自己測試有效果
 

37             //獲取HttpServletResponse對象
38             HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
39             String webRoot = request.getContextPath();
40             if (page.startsWith(webRoot)) {
41                 //重定向到page頁面
42                 response.sendRedirect(page);
43             } else {
44                 //重定向到page頁面
45                 response.sendRedirect(webRoot+page);
46             }
47             //重定向後,控制保護的頁面不要執行
48             throw new SkipPageException();
49         }
50     }
51 
52     public void setSite(String site) {
53         this.site = site;
54     }
55 
56     public void setPage(String page) {
57         this.page = page;
58     }
59 }
複製代碼

  2、在WEB-INF目錄下tld文件中添加對該標籤的描述,如下:

複製代碼
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
 6     
 7     <description>孤傲蒼狼開發的簡單標籤庫</description>
 8     <tlib-version>1.0</tlib-version>
 9     <short-name>TagLib</short-name>
10     <uri>/gaclTagLib</uri>
11     
12     <tag>
13         <!-- 標籤名 -->
14         <name>referer</name>
15         <!-- 標籤處理器類 -->
16         <tag-class>me.gacl.web.simpletag.RefererTag</tag-class>
17         <!-- 標籤體允許的內容 -->
18         <body-content>empty</body-content>
19         <!-- 標籤的屬性描述 -->
20         <attribute>
21             <description>描述標籤的site屬性</description>
22             <!-- 標籤的site屬性 -->
23             <name>site</name>
24             <required>true</required>
25             <!-- rtexprvalue用來指示標籤的屬性值是否可以是一個表達式, 一般設置爲true,true就表示允許標籤的屬性值可以是一個表達式 -->
26             <rtexprvalue>true</rtexprvalue>
27         </attribute>
28         <attribute>
29             <description>描述標籤的page屬性</description>
30             <!-- 標籤的page屬性 -->
31             <name>page</name>
32             <required>true</required>
33             <!-- rtexprvalue用來指示標籤的屬性值是否可以是一個表達式, 一般設置爲true,true就表示允許標籤的屬性值可以是一個表達式 -->
34             <rtexprvalue>true</rtexprvalue>
35         </attribute>
36     </tag>
37     
38 </taglib>
複製代碼

  3、測試:在jsp頁面中導入標籤庫並使用防盜鏈標籤

複製代碼
 1 <%@ page language="java" pageEncoding="UTF-8"%>
 2 <%--在jsp頁面中導入自定義標籤庫 --%>
 3 <%@taglib uri="/gaclTagLib" prefix="gacl" %>
 4 <%--在jsp頁面中也可以使用這種方式導入標籤庫,直接把uri設置成標籤庫的tld文件所在目錄
 5 <%@taglib uri="/WEB-INF/TagLib.tld" prefix="gacl"%>
 6 --%>
 7 <%--在Jsp頁面中使用防盜鏈標籤 
 8 當用戶嘗試直接通過URL地址(http://localhost:8080/JavaWeb_JspTag_study_20140816/simpletag/refererTagTest.jsp)訪問這個頁面時,
 9 防盜鏈標籤的標籤處理器內部就會進行處理,將請求重定向到/index.jsp
10 --%>
11 <gacl:referer site="http://localhost:8080" page="/index.jsp"/>
12 <!DOCTYPE HTML>
13 <html>
14   <head>
15     <title>防盜鏈標籤測試</title>
16   </head>
17 
18   <body>
19      網站內部資料
20   </body>
21 </html>
複製代碼

  運行效果如下:

  

1.2、開發<c:if>標籤

  1、編寫標籤處理器類:IFTag.java

複製代碼
 1 package me.gacl.web.simpletag;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.jsp.JspException;
 5 import javax.servlet.jsp.tagext.SimpleTagSupport;
 6 
 7 /**
 8  * @author gacl
 9  * 開發if標籤
10  */
11 public class IFTag extends SimpleTagSupport {
12 
13     /**
14      * if標籤的test屬性
15      */
16     private boolean test;
17     
18     @Override
19     public void doTag() throws JspException, IOException {
20         if (test) {
21             this.getJspBody().invoke(null);
22         }
23     }
24 
25     public void setTest(boolean test) {
26         this.test = test;
27     }
28 }
複製代碼

  2、在WEB-INF目錄下tld文件中添加對該標籤的描述,如下:

複製代碼
 1 <tag>
 2     <description>if標籤</description>
 3     <name>if</name>
 4     <tag-class>me.gacl.web.simpletag.IFTag</tag-class>
 5     <body-content>scriptless</body-content>
 6     <attribute>
 7         <description>if標籤的test屬性</description>
 8         <name>test</name>
 9         <rtexprvalue>true</rtexprvalue>
10         <required>true</required>
11     </attribute>
12 </tag>
複製代碼

  3、測試:在jsp頁面中導入標籤庫並使用if標籤

複製代碼
 1 <%@ page language="java" pageEncoding="UTF-8"%>
 2 <%--在jsp頁面中導入自定義標籤庫 --%>
 3 <%@taglib uri="/gaclTagLib" prefix="c" %>
 4 <%--在jsp頁面中也可以使用這種方式導入標籤庫,直接把uri設置成標籤庫的tld文件所在目錄
 5 <%@taglib uri="/WEB-INF/TagLib.tld" prefix="c"%>
 6 --%>
 7 <!DOCTYPE HTML>
 8 <html>
 9   <head>
10     <title>if鏈標籤測試</title>
11   </head>
12 
13   <body>
14   <%--if標籤的test屬性值爲true ,標籤體的內容會輸出--%>
15       <c:if test="true">
16          <h3>網站內部資料</h3>
17     </c:if>
18     <%--if標籤的test屬性值爲false ,標籤體的內容不會輸出--%>
19     <c:if test="false">
20         這裏的內部不輸出
21     </c:if>
22   </body>
23 </html>
複製代碼

  運行效果如下:

  

1.3、開發<c:when><c:otherwise>標籤

  這個標籤的開發稍微有一點難度,因爲這裏面涉及到兩個標籤處理器類共享同一個變量的問題,如下:

1 <c:when test="${user != null}">
2     用戶不爲空
3 </c:when>
4 <c:otherwise>
5     用戶爲空
6 </c:otherwise>

  <c:when>標籤和<c:otherwise>標籤對應着兩個不同的標籤處理器類,我們希望做到的效果是,如果<c:when>標籤執行了,那麼就<c:otherwise>標籤就不要再執行,那麼這裏面就涉及到一個問題:<c:when>標籤執行的時候該如何通知<c:otherwise>標籤不要執行了呢?這個問題就涉及到了兩個標籤處理器類如何做到相互通訊的問題,如果<c:when>標籤執行了,就要通過某種方式告訴<c:otherwise>標籤不要執行,那麼該如何做到這樣的效果呢?讓<c:when>標籤處理器類和<c:otherwise>標籤處理器類共享同一個變量就可以做到了,那麼又該怎麼做才能夠讓兩個標籤處理器類共享同一個變量呢,標準的做法是這樣的:讓兩個標籤擁有同一個父標籤。

 1、開發父標籤:ChooseTag.java

複製代碼
 1 package me.gacl.web.simpletag;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.jsp.JspException;
 5 import javax.servlet.jsp.tagext.SimpleTagSupport;
 6 
 7 /**
 8  * @author gacl
 9  * when標籤和otherwise標籤的父標籤
10  */
11 public class ChooseTag extends SimpleTagSupport {
12 
13     /**
14      * 定義一個boolean類型的屬性,該屬性用於標識該標籤下的某一個子標籤是否已經執行過了,
15      * 如果該標籤下的某一個子標籤已經執行過了,就將該屬性設置爲true
16      */
17     private boolean isExecute;
18     
19     @Override
20     public void doTag() throws JspException, IOException {
21         //輸出標籤體中的內容
22         this.getJspBody().invoke(null);
23     }
24 
25     public boolean isExecute() {
26         return isExecute;
27     }
28 
29     public void setExecute(boolean isExecute) {
30         this.isExecute = isExecute;
31     }
32 }
複製代碼

  2、開發when標籤和otherwise標籤

  WhenTag.java

複製代碼
 1 package me.gacl.web.simpletag;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.jsp.JspException;
 5 import javax.servlet.jsp.tagext.SimpleTagSupport;
 6 
 7 /**
 8  * @author gacl
 9  * when標籤
10  */
11 public class WhenTag extends SimpleTagSupport {
12 
13     /**
14      * test屬性,該屬性值爲true時,輸出標籤體中的內容
15      */
16     private boolean test;
17複製代碼
 1 package me.gacl.web.simpletag;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.jsp.JspException;
 5 import javax.servlet.jsp.tagext.SimpleTagSupport;
 6 
 7 /**
 8  * @author gacl
 9  * when標籤
10  */
11 public class WhenTag extends SimpleTagSupport {
12 
13     /**
14      * test屬性,該屬性值爲true時,輸出標籤體中的內容
15      */
16     private boolean test;
17     
18     @Override
19     public void doTag() throws JspException, IOException {
20         //獲取標籤的父標籤
21         ChooseTag parentTag = (ChooseTag) this.getParent();
22         if (test == true && parentTag.isExecute() == false) {
23             //輸出標籤體中的內容
24             this.getJspBody().invoke(null);
25             //將父標籤的isExecute屬性設置爲true,告訴父標籤,我(when標籤)已經執行過了
26             parentTag.setExecute(true);
27         }
28     }
29 
30     public void setTest(boolean test) {
31         this.test = test;
32     }
33 }
複製代碼

  OtherWiseTag.java

複製代碼
 1 package me.gacl.web.simpletag;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.jsp.JspException;
 5 import javax.servlet.jsp.tagext.SimpleTagSupport;
 6 
 7 /**
 8  * @author gacl
 9  * otherwise標籤
10  */
11 public class OtherWiseTag extends 6 
 7 /**
 8  * @author gacl
 9  * when標籤
10  */
11 public class WhenTag extends SimpleTagSupport {
12 
13     /**
14      * test屬性,該屬性值爲true時,輸出標籤體中的內容
15      */
16     private boolean test;
17     
18     @Override
19     public void doTag() throws JspException, IOException {
20         //獲取標籤的父標籤
21         ChooseTag parentTag = (ChooseTag) this.getParent();
22         if (test == true && parentTag.isExecute() == false) {
23             //輸出標籤體中的內容
24             this.getJspBody().invoke(null);
25             //將父標籤的isExecute屬性設置爲true,告訴父標籤,我(when標籤)已經執行過了
26             parentTag.setExecute(true);
27         }
28     }
29 
30     public void setTest(boolean test) {
31         this.test = test;
32     }
33 }
複製代碼

  OtherWiseTag.java

複製代碼
 1 package me.gacl.web.simpletag;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.jsp.JspException;
 5 import javax.servlet.jsp.tagext.SimpleTagSupport;
 6 
 7 /**
 8  * @author gacl
 9  * otherwise標籤
10  */
11 public class OtherWiseTag extends SimpleTagSupport {
12 
13     @Override
14     public true && parentTag.isExecute() == false) {
23             //輸出標籤體中的內容
24             this.getJspBody().invoke(null);
25             
相關文章
相關標籤/搜索