<?xml version="1.0" encoding="UTF-8"?>java
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">web
<!--taglib(標籤庫)的版本號 -->數據庫
<!--縮寫,沒用到-->express
<!-- apache
爲自定義標籤庫設置一個uri,uri以/開頭,/後面的內容隨便寫,如這裏的/gacl ,
在Jsp頁面中引用標籤庫時,須要經過uri找到標籤庫
在Jsp頁面中就要這樣引入標籤庫:<%@taglib uri="/gacl" prefix="gacl"%>
-->
<!-- description用來添加對taglib(標籤庫)的描述 -->緩存
<!--一個taglib(標籤庫)中包含多個自定義標籤,每個自定義標籤使用一個tag標記來描述 -->服務器
<!-- 一個tag標記對應一個自定義標籤 -->app
<tag>less
<name>getName</name>//
<tag-class>com.maystar.tag.TestTag</tag-class>
<body-content>JSP</body-content>
<description><![CDATA[validate the element can be show ]]></description>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>jsp
</taglib>
private static PageContext pageContext;
private String value;
this.value = value;
@Override
public int doStartTag() throws JspException {
HttpServletRequest request =(HttpServletRequest) pageContext.getRequest();
Map map = (Map)request.getSession().getServletContext().getAttribute("MAP_ROLE");
String str1=Convert.trimNull(map.get(value));
JspWriter out = pageContext.getOut();
try {
//這裏輸出的時候會拋出IOException異常
out.write(str1);
} catch (IOException e) {
//捕獲IOException異常後繼續拋出
throw new RuntimeException(e);
}
return 0;
}
@Override
public int doEndTag() throws JspException {
System.out.println("調用doEndTag()方法");
return 0;
}
@Override
public TagSupport getParent() {
return null;
}
@Override
public void release() {
System.out.println("調用release()方法");
}
@Override
public void setPageContext(PageContext pageContext) {
System.out.println("setPageContext(PageContext pageContext)");
this.pageContext = pageContext;
}
public void setParent(TagSupport arg0) {
}
一、public void setPageContext(PageContext pc), JSP引擎實例化標籤處理器後,將調用setPageContext方法將JSP頁面的pageContext對象傳遞給標籤處理器,標籤處理器之後能夠經過這個pageContext對象與JSP頁面進行通訊。
二、public void setParent(Tag t),setPageContext方法執行完後,WEB容器接着調用的setParent方法將當前標籤的父標籤傳遞給當前標籤處理器,若是當前標籤沒有父標籤,則傳遞給setParent方法的參數值爲null。
三、public int doStartTag(),調用了setPageContext方法和setParent方法以後,WEB容器執行到自定義標籤的開始標記時,就會調用標籤處理器的doStartTag方法。
四、public int doEndTag(),WEB容器執行完自定義標籤的標籤體後,就會接着去執行自定義標籤的結束標記,此時,WEB容器會去調用標籤處理器的doEndTag方法。
五、public void release(),一般WEB容器執行完自定義標籤後,標籤處理器會駐留在內存中,爲其它請求服務器,直至中止web應用時,web容器纔會調用release方法。
<?xml version="1.0" encoding="UTF-8" ?>
<!--
<function>
<description>測試: 與 el 結合使用 如 ${test:getStringLength("XX")}</description>
<name>getStringLength</name> 注意,這裏,定義的是,標籤對應的函數名
<function-class>taglib.StringUtils</function-class>注意:這裏,給出了,該類,所在的路徑,全路徑
<function-signature>java.lang.Integer getStringLength(java.lang.String)</function-signature>
</function>
</taglib>
<jsp-config>
<taglib>
</taglib>
</jsp-config>