一、自定義標簽的作用
自定義標簽主要用於移除Jsp頁面中的java代碼。
二、自定義標簽開發和使用
2.1、自定義標簽開發步驟
1、編寫一個實現Tag接口的Java類(標簽處理器類)
1 package me.gacl.web.tag; 2 3 import java.io.IOException; 4 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.jsp.JspException; 7 import javax.servlet.jsp.JspWriter; 8 import javax.servlet.jsp.PageContext; 9 import javax.servlet.jsp.tagext.Tag; 10 11 public class ViewIPTag implements Tag { 12 13 //接收傳遞進來的PageContext對象 14 private PageContext pageContext; 15 16 @Override 17 public int doEndTag() throws JspException { 18 System.out.println("調用doEndTag()方法"); 19 return 0; 20 } 21 22 @Override 23 public int doStartTag() throws JspException { 24 System.out.println("調用doStartTag()方法"); 25 HttpServletRequest request =(HttpServletRequest) pageContext.getRequest(); 26 JspWriter out = pageContext.getOut(); 27 String ip = request.getRemoteAddr(); 28 try { 29 //這里輸出的時候會拋出IOException異常 30 out.write(ip); 31 } catch (IOException e) { 32 //捕獲IOException異常后繼續拋出 33 throw new RuntimeException(e); 34 } 35 return 0; 36 } 37 38 @Override 39 public Tag getParent() { 40 return null; 41 } 42 43 @Override 44 public void release() { 45 System.out.println("調用release()方法"); 46 } 47 48 @Override 49 public void setPageContext(PageContext pageContext) { 50 System.out.println("setPageContext(PageContext pageContext)"); 51 this.pageContext = pageContext; 52 } 53 54 @Override 55 public void setParent(Tag arg0) { 56 57 } 58 59 }
2、在WEB-INF/目錄下新建tld文件,在tld文件中對標簽處理器類進行描述
gacl.tld文件的代碼如下:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 <taglib 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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" 6 version="2.0"> 7 <!-- description用來添加對taglib(標簽庫)的描述 --> 8 <description>孤傲蒼狼開發的自定義標簽庫</description> 9 <!--taglib(標簽庫)的版本號 --> 10 <tlib-version>1.0</tlib-version> 11 <short-name>GaclTagLibrary</short-name> 12 <!-- 13 為自定義標簽庫設置一個uri,uri以/開頭,/后面的內容隨便寫,如這里的/gacl , 14 在Jsp頁面中引用標簽庫時,需要通過uri找到標簽庫 15 在Jsp頁面中就要這樣引入標簽庫:<%@taglib uri="/gacl" prefix="gacl"%> 16 --> 17 <uri>/gacl</uri> 18 19 <!--一個taglib(標簽庫)中包含多個自定義標簽,每一個自定義標簽使用一個tag標記來描述 --> 20 <!-- 一個tag標記對應一個自定義標簽 --> 21 <tag> 22 <description>這個標簽的作用是用來輸出客戶端的IP地址</description> 23 <!-- 24 為標簽處理器類配一個標簽名,在Jsp頁面中使用標簽時是通過標簽名來找到要調用的標簽處理器類的 25 通過viewIP就能找到對應的me.gacl.web.tag.ViewIPTag類 26 --> 27 <name>viewIP</name> 28 <!-- 標簽對應的處理器類--> 29 <tag-class>me.gacl.web.tag.ViewIPTag</tag-class> 30 <body-content>empty</body-content> 31 </tag> 32 33 </taglib>
2.2、在Jsp頁面中使用自定義標簽
1、使用"<%@taglib uri="標簽庫的uri" prefix="標簽的使用前綴"%>"指令引入要使用的標簽庫。
例如:在jspTag_Test1.jsp中引用gacl標簽庫
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <!-- 使用taglib指令引用gacl標簽庫,標簽庫的前綴(prefix)可以隨便設置,如這里設置成 prefix="xdp" --> 3 <%@taglib uri="/gacl" prefix="xdp"%> 4 <!DOCTYPE HTML> 5 <html> 6 <head> 7 <title>輸出客戶端的IP</title> 8 </head> 9 10 <body> 11 你的IP地址是(使用java代碼獲取輸出): 12 <% 13 //在jsp頁面中使用java代碼獲取客戶端IP地址 14 String ip = request.getRemoteAddr(); 15 out.write(ip); 16 %> 17 <hr/> 18 你的IP地址是(使用自定義標簽獲取輸出): 19 <%--使用自定義標簽viewIP --%> 20 <xdp:viewIP/> 21 </body> 22 </html>
標簽的運行效果如下:
從運行效果種可以看到,使用自定義標簽就可以將jsp頁面上的java代碼移除掉,如需要在jsp頁面上輸出客戶端的IP地址時,使用 <xdp:viewIP/>標簽就可以代替jsp頁面上的這些代碼:
1 <% 2 //在jsp頁面中使用java代碼獲取客戶端IP地址 3 String ip = request.getRemoteAddr(); 4 out.write(ip); 5 %>
這就是開發和使用自定義標簽的好處,可以讓我們的Jsp頁面上不嵌套java代碼。
三、自定義標簽的執行流程
JSP引擎遇到自定義標簽時,首先創建標簽處理器類的實例對象,然后按照JSP規范定義的通信規則依次調用它的方法。
1、public void setPageContext(PageContext pc), JSP引擎實例化標簽處理器后,將調用setPageContext方法將JSP頁面的pageContext對象傳遞給標簽處理器,標簽處理器以后可以通過這個pageContext對象與JSP頁面進行通信。
2、public void setParent(Tag t),setPageContext方法執行完后,WEB容器接着調用的setParent方法將當前標簽的父標簽傳遞給當前標簽處理器,如果當前標簽沒有父標簽,則傳遞給setParent方法的參數值為null。
3、public int doStartTag(),調用了setPageContext方法和setParent方法之后,WEB容器執行到自定義標簽的開始標記時,就會調用標簽處理器的doStartTag方法。
4、public int doEndTag(),WEB容器執行完自定義標簽的標簽體后,就會接着去執行自定義標簽的結束標記,此時,WEB容器會去調用標簽處理器的doEndTag方法。
5、public void release(),通常WEB容器執行完自定義標簽后,標簽處理器會駐留在內存中,為其它請求服務器,直至停止web應用時,web容器才會調用release方法。
我們在tomcat服務器的"work\Catalina\localhost\JavaWeb_JspTag_study_20140816\org\apache\jsp"目錄下可以找到將jspTag_Test1.jsp翻譯成Servlet后的java源代碼,如下圖所示:
打開jspTag_005fTest1_jsp.java文件,可以看到setPageContext(PageContext pc)、setParent(Tag t)、doStartTag()、doEndTag()、release()這5個方法的調用順序和過程。
jspTag_005fTest1_jsp.java的代碼如下:
1 package org.apache.jsp; 2 3 import javax.servlet.*; 4 import javax.servlet.http.*; 5 import javax.servlet.jsp.*; 6 7 public final class jspTag_005fTest1_jsp extends org.apache.jasper.runtime.HttpJspBase 8 implements org.apache.jasper.runtime.JspSourceDependent { 9 10 private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory(); 11 12 private static java.util.List _jspx_dependants; 13 14 static { 15 _jspx_dependants = new java.util.ArrayList(1); 16 _jspx_dependants.add("/WEB-INF/gacl.tld"); 17 } 18 19 private org.apache.jasper.runtime.TagHandlerPool _005fjspx_005ftagPool_005fxdp_005fviewIP_005fnobody; 20 21 private javax.el.ExpressionFactory _el_expressionfactory; 22 private org.apache.AnnotationProcessor _jsp_annotationprocessor; 23 24 public Object getDependants() { 25 return _jspx_dependants; 26 } 27 28 public void _jspInit() { 29 _005fjspx_005ftagPool_005fxdp_005fviewIP_005fnobody = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(getServletConfig()); 30 _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); 31 _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName()); 32 } 33 34 public void _jspDestroy() { 35 _005fjspx_005ftagPool_005fxdp_005fviewIP_005fnobody.release(); 36 } 37 38 public void _jspService(HttpServletRequest request, HttpServletResponse response) 39 throws java.io.IOException, ServletException { 40 41 PageContext pageContext = null; 42 HttpSession session = null; 43 ServletContext application = null; 44 ServletConfig config = null; 45 JspWriter out = null; 46 Object page = this; 47 JspWriter _jspx_out = null; 48 PageContext _jspx_page_context = null; 49 50 51 try { 52 response.setContentType("text/html;charset=UTF-8"); 53 pageContext = _jspxFactory.getPageContext(this, request, response, 54 null, true, 8192, true); 55 _jspx_page_context = pageContext; 56 application = pageContext.getServletContext(); 57 config = pageContext.getServletConfig(); 58 session = pageContext.getSession(); 59 out = pageContext.getOut(); 60 _jspx_out = out; 61 62 out.write("\r\n"); 63 out.write("<!-- 引用gacl標簽庫,標簽庫的前綴(prefix)可以隨便設置,如這里設置成 prefix=\"gacl\" -->\r\n"); 64 out.write("\r\n"); 65 out.write("<!DOCTYPE HTML>\r\n"); 66 out.write("<html>\r\n"); 67 out.write(" <head>\r\n"); 68 out.write(" <title>輸出客戶端的IP</title>\r\n"); 69 out.write(" </head>\r\n"); 70 out.write(" \r\n"); 71 out.write(" <body>\r\n"); 72 out.write(" 你的IP地址是(使用java代碼獲取輸出):\r\n"); 73 out.write(" "); 74 75 //在jsp頁面中使用java代碼獲取客戶端IP地址 76 String ip = request.getRemoteAddr(); 77 out.write(ip); 78 79 out.write("\r\n"); 80 out.write(" <hr/>\r\n"); 81 out.write(" 你的IP地址是(使用自定義標簽獲取輸出):"); 82 if (_jspx_meth_xdp_005fviewIP_005f0(_jspx_page_context)) 83 return; 84 out.write("\r\n"); 85 out.write(" </body>\r\n"); 86 out.write("</html>\r\n"); 87 } catch (Throwable t) { 88 if (!(t instanceof SkipPageException)){ 89 out = _jspx_out; 90 if (out != null && out.getBufferSize() != 0) 91 try { out.clearBuffer(); } catch (java.io.IOException e) {} 92 if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); 93 } 94 } finally { 95 _jspxFactory.releasePageContext(_jspx_page_context); 96 } 97 } 98 99 private boolean _jspx_meth_xdp_005fviewIP_005f0(PageContext _jspx_page_context) 100 throws Throwable { 101 PageContext pageContext = _jspx_page_context; 102 JspWriter out = _jspx_page_context.getOut(); 103 // xdp:viewIP 104 me.gacl.web.tag.ViewIPTag _jspx_th_xdp_005fviewIP_005f0 = (me.gacl.web.tag.ViewIPTag) _005fjspx_005ftagPool_005fxdp_005fviewIP_005fnobody.get(me.gacl.web.tag.ViewIPTag.class); 105 _jspx_th_xdp_005fviewIP_005f0.setPageContext(_jspx_page_context); 106 _jspx_th_xdp_005fviewIP_005f0.setParent(null); 107 int _jspx_eval_xdp_005fviewIP_005f0 = _jspx_th_xdp_005fviewIP_005f0.doStartTag(); 108 if (_jspx_th_xdp_005fviewIP_005f0.doEndTag() == javax.servlet.jsp.tagext.Tag.SKIP_PAGE) { 109 _005fjspx_005ftagPool_005fxdp_005fviewIP_005fnobody.reuse(_jspx_th_xdp_005fviewIP_005f0); 110 return true; 111 } 112 _005fjspx_005ftagPool_005fxdp_005fviewIP_005fnobody.reuse(_jspx_th_xdp_005fviewIP_005f0); 113 return false; 114 } 115 }
下面重點分析一下上述代碼中標紅色的那個 private boolean _jspx_meth_xdp_005fviewIP_005f0(PageContext _jspx_page_context)方法中的代碼
①、這里是實例化一個viewIP標簽處理器類me.gacl.web.tag.ViewIPTag的對象
1 // xdp:viewIP 2 me.gacl.web.tag.ViewIPTag _jspx_th_xdp_005fviewIP_005f0 = (me.gacl.web.tag.ViewIPTag) _005fjspx_005ftagPool_005fxdp_005fviewIP_005fnobody.get(me.gacl.web.tag.ViewIPTag.class);
②、實例化標簽處理器后,調用setPageContext方法將JSP頁面的pageContext對象傳遞給標簽處理器
1 _jspx_th_xdp_005fviewIP_005f0.setPageContext(_jspx_page_context);
③、setPageContext方法執行完后,接着調用的setParent方法將當前標簽的父標簽傳遞給當前標簽處理器,如果當前標簽沒有父標簽,則傳遞給setParent方法的參數值為null
1 _jspx_th_xdp_005fviewIP_005f0.setParent(null);
④、調用了setPageContext方法和setParent方法之后,WEB容器執行到自定義標簽的開始標記時,就會調用標簽處理器的doStartTag方法
1 int _jspx_eval_xdp_005fviewIP_005f0 = _jspx_th_xdp_005fviewIP_005f0.doStartTag();
⑤、WEB容器執行完自定義標簽的標簽體后,就會接着去執行自定義標簽的結束標記,此時,WEB容器會去調用標簽處理器的doEndTag方法
1 if (_jspx_th_xdp_005fviewIP_005f0.doEndTag() == javax.servlet.jsp.tagext.Tag.SKIP_PAGE)
這就是自定義標簽的執行流程。
這里以一個入門級的案例來講解javaweb的自定義標簽開發,在后面的博文中會進行更加詳盡的介紹。