關於JSP的自定義標籤
前兩天學習了JSP的自定義標籤 ,現在來記述下,加深印象。
一、建立自定義標籤類
自定義標籤類的建立方式有多種,不過還是繼承SimpleTagSupport類比較簡單,沒必要重寫那麼多方法,只需要重寫doTag方法,然後把邏輯處理寫在doTag方法裡。此處是我的三個自定義標籤處理類:
ChooseTag 類:
package com.gao.simpleTag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; import java.io.IOException; public class ChooseTag extends SimpleTagSupport { private boolean flag; public void setFlag(boolean flag) { this.flag = flag; } public boolean getFlag() { return flag; } @Override public void doTag() throws JspException, IOException { getJspBody().invoke(null); } }
WhenTag類:
package com.gao.simpleTag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; import java.io.IOException; public class WhenTag extends SimpleTagSupport { private boolean test =false; public void setTest(boolean test) { this.test = test; } @Override public void doTag() throws JspException, IOException { if (test){ getJspBody().invoke(null); ChooseTag chooseTag = (ChooseTag) getParent(); chooseTag.setFlag(true); } } }
OtherWiseTag類:
package com.gao.simpleTag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; import java.io.IOException; public class OtherWiseTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { ChooseTag chooseTag = (ChooseTag) getParent(); if (!chooseTag.getFlag()){ getJspBody().invoke(null); } } }
自定義標籤類的父類SimpleTagSupport裡的幾個成員變數及其對應的set、get方法
第一個變數parentTag是用來獲取父標籤的;
第二個變數jspContext是類JspContext例項,JspContext類作為PageContext的父類可以用來獲取JSP上下文的資訊;
第三個變數jspBody是類JspFragment的例項,該例項可以作為jsp頁面的物件,用來返回Jsp頁面的一段符合Jsp語法規範的Jsp程式碼。JspFragment類提供了 public abstract void invoke( Writer out ) throws JspException, IOException
和public abstract JspContext getJspContext();兩個 抽象方法。invoke()方法用來對標籤內容在修改、或者其他操作後是否輸出到頁面上或者跳過執行(如果不呼叫該方法,則跳過標籤體的內容,)。getJspContext()方法獲取一個JspContext例項物件.
二、建立自定義標籤配置檔案(*.tld)
<?xml version="1.0" encoding="UTF-8"?>
<taglib 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"
version="2.0">
<description>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>SimpleTag</short-name>
<uri>/mySimpleTag</uri>
<tag>
<name>chooseTag</name>
<tag-class>com.gao.simpleTag.ChooseTag</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>whenTag</name>
<tag-class>com.gao.simpleTag.WhenTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>otherWiseTag</name>
<tag-class>com.gao.simpleTag.OtherWiseTag</tag-class>
<body-content>scriptless</body-content>
</tag>
</taglib>
該配置檔案的格式如上,下面說明幾個標籤的含義:
1、<description> 對該標籤庫的描述(可自定義)
2、<tlib-version>該標籤庫的版本(可自定義)
3、<short-name>該標籤庫的縮略名(可自定義)
4、<uri> 該標籤庫的地址URI,該值在引用該標籤時需要用到,所以值儘量有意義
5、<tag>對應標籤庫裡的具體標籤
① <name> 該標籤的名稱,該值在引用是需要使用
②<tag-class> 處理該標籤的邏輯類,此處為處理類的全名引用地址
③<body-content> 內容部分,此處有幾個可選值。empty、scrptless、JSP、tagdependment
empty:空標記,即起始標記和結束標記之間沒有內容。
scriptless:接受文字、EL和JSP動作.不支援Java程式碼
JSP:接受所有JSP語法,如定製的或內部的tag、scripts、靜態HTML、指令碼元素、JSP指令和動作。
tagdependent:標籤體內容直接被寫入BodyContent,由自定義標籤類來進行處理,而不被JSP容器解釋。
④<attribute>該標籤允許自定義標籤內新增元素,標籤內包含幾個子標籤,如<name>、<required>、<rtexprvalue>、 <type>、<fragment>,下面是官方API給的屬性說明:
description a description of the attribute
name the name of the attribute
required whether the attribute is required or optional
rtexprvalue whether the attribute is a runtime attribute
type the type of the attributes
fragment whether this attribute is a fragment
三、在Jsp頁面引用及使用
1、在Jsp頁面頭部對標籤庫進行引用,引用格式如下:
<%@taglib prefix="mySimpleTag" uri="/mySimpleTag" %>
其中uri對應我們的*.tld配置檔案中的uri即可。
使用標籤是方法如下:
<%@taglib prefix="mySimpleTag" uri="/mySimpleTag" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
pageContext.setAttribute("age",22);
%>
<mySimpleTag:chooseTag>
<mySimpleTag:whenTag test="${age >= 18}">
<h3>你已經成年了</h3>
</mySimpleTag:whenTag>
<mySimpleTag:otherWiseTag>
<h3>小朋友,快快長大哦</h3>
</mySimpleTag:otherWiseTag>
</mySimpleTag:chooseTag>
</body>
</html>
執行結果如下:
記述:雖然該技術很早就已經出現了,不過到現在為止此技術仍經常被使用,希望以後當自己每次翻起此文章時能給自己點靈感和想法,也希望給剛使用次技術的新同學經驗。