自訂標籤(JSTL)

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   使用   java   

自訂標籤的步驟:

  1、確定需求,如:用<my:date/>輸出目前時間

  2、編寫Java類:需要實現實現介面javax.servlet.jsp.tagext.JspTag

          具體的介面為:

              javax.servlet.jsp.tagext.SimpleTag,簡單標籤,JSP2.0**
                      javax.servlet.jsp.tagext.Tag,經典標籤

 

  3、編寫tdl檔案(xml)

 

Demo:

 1 public class MyTag implements SimpleTag { 2      3     private PageContext pageContext; 4     public void setJspContext(JspContext pc) { 5          6         this.pageContext=(PageContext) pc;//設定pageContext 7     } 8      9     public void doTag() throws JspException, IOException {10         11         SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss:SSS");12         String date=format.format(new Date());13         pageContext.getOut().print(date);//輸出到頁面14     }15 16     public JspTag getParent() {17         // TODO Auto-generated method stub18         return null;19     }20 21     public void setJspBody(JspFragment arg0) {22         // TODO Auto-generated method stub23         24     }


 1 <?xml version="1.0" encoding="UTF-8" ?> 2  3 <taglib xmlns="http://java.sun.com/xml/ns/javaee" 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" 6     version="2.1">

    <!-- 確定當前標籤庫的版本 --> 7 <tlib-version>1.0</tlib-version> 8 <short-name>my</short-name> <!-- 建議引入當前便簽庫是,使用的簡寫--> 9 <uri>http://www.cnblogs.com/liuwt365/mytag</uri> <!-- 為當前標籤庫設定檔,起一個名稱        * 內容:自訂,全球唯一         -->10 11 <tag>12 <name>date</name> <!-- 確定標籤名稱 -->13 <tag-class>cn.test.simpleTag.MyTag</tag-class> <!-- 確定實作類別 -->14 <body-content>empty</body-content> <!-- 格式化當前標籤的body內容-->15 </tag>16 17 </taglib>
1 <%@ taglib uri="http://www.cnblogs.com/liuwt365/mytag" prefix="my" %>   <!--  頁面註冊  -->2 3 <my:date/>

PS:如果標籤中含有屬性,則tdl中需要設定:<my:maxValue num1="13" num2="15"></my:maxValue>

  

 1 <tag> 2         <name>max</name> 3         <tag-class>cn.test.simpleTag.MyMaxTag</tag-class> 4         <body-content>empty</body-content> 5         <attribute> 6             <name>num1</name> 7             <required>true</required> 8             <rtexprvalue>true</rtexprvalue> 9             <type>java.lang.Integer</type>10         </attribute>11         12         <attribute>//設定屬性13             <name>num2</name>//屬性名稱14             <required>true</required>//是否必須  true|false    (yes|no)(非必須)15             <rtexprvalue>true</rtexprvalue>  //runtime expression value  運行時運算式16             <type>java.lang.Integer</type>   //類型(非必須)17         </attribute>18     </tag>

  如果含有標籤體,tdl中設定如下:<my:toUpper>JUHjdksds</my:toUpper>

<tag>        <name>toUpper</name>        <tag-class>cn.test.simpleTag.MyToUpperTag</tag-class>        <body-content>scriptless</body-content>

<!-- 確定標籤體的類型
            * empty:沒有標籤體
            * JSP:支援JSP所有的文法,在Tag中使用
            * scriptless:對於JSP頁面的指令碼不支援 <%%>
            * tagdependent:標籤體不做處理,原樣輸出
-->
</tag>

如果含有標籤體 ,java類中的

1 public void setJspBody(JspFragment jf) {2         this.jspFragment=jf;   //擷取     3     }

向jsp頁面輸出時

1 StringWriter stringWriter=new StringWriter();//獲得流2         3         this.jspFragment.invoke(stringWriter);//將資料寫入到流中4         String data=stringWriter.toString().toUpperCase();5         6         this.pageContext.getOut().print(data);7         
this.jspFragment.invoke(pageContext.getOut())=this.jspFragment.invoke();

自訂標籤(JSTL)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.