jsp自定義標簽之ifelse與遍歷自定義標簽示例
來源:易賢網(wǎng) 閱讀:877 次 日期:2015-02-04 15:04:22
溫馨提示:易賢網(wǎng)小編為您整理了“jsp自定義標簽之ifelse與遍歷自定義標簽示例”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了jsp自定義標簽之ifelse與遍歷自定義標簽,需要的朋友可以參考下

第一個示例:

簡單的jsp自定標簽獲取內(nèi)容:

首先創(chuàng)建一個jsp實例類然后繼承simpletagsupport類

然后實現(xiàn)父類的dotag()方法

在這個方法里獲取標簽體里的內(nèi)容this.getjspbody();

返回的是jspfragment 類,根據(jù)這個類對象調(diào)用invoke(this.getjspcontext().getout());

這個方法里面也可以寫空,所表達的意思也是輸出到瀏覽器;

代碼如下:

public class simpledmeo1 extends simpletagsupport {

@override

public void dotag() throws jspexception, ioexception {

jspfragment js =this.getjspbody();

js.invoke(null);

}

}

然后在寫tld文件標簽庫描述文件,和jsp文件,這些都較為簡單

如果不想執(zhí)行某個內(nèi)容就拋出異常

throw new skippageexception();和面內(nèi)容就不會顯示

接下來是一個帶屬性的jsp自定義標簽文件

代碼如下:

public class simpledmeo1 extends simpletagsupport {

private int counts;

public void setcounts(int counts) {

this.counts = counts;

}

@override

public void dotag() throws jspexception, ioexception {

jspfragment js =this.getjspbody();

for(int i=0;i<counts;i++){ //循環(huán)獲取

js.invoke(null);

}

}

}

<description>a tag library exercising simpletag handlers.</description>

<tlib-version>1.0</tlib-version>

<short-name>c</short-name>前綴名

<uri>http://www.csdn.com</uri>

<tag>

<name>demo</name>

<tag-class>com.csdn.simple.simpledmeo1</tag-class>

<body-content>scriptless</body-content>

<attribute>

<name>counts</name>

<required>true</required>

<rtexprvalue>true</rtexprvalue>

</attribute>

</tag>

然后再jsp文件 中寫出內(nèi)容;

代碼如下:

<hbsi:demo counts=3>aaaaaaa<br/></hbsi:demo> //輸出三編

jspfragment js = this.getjspbody();

stringwriter jw = new stringwriter();

js.invoke(jw);

string s = jw.tostring().touppercase();

jspwriter out =this.getjspcontext().getout();

for(int i=0;i<counts2;i++){

out.print(s);

}

}

這是轉(zhuǎn)成大寫的代碼,其他的都一致;

關(guān)于if else的代碼,太多,我放到資源里了,有必要的話可以下載下來,僅供參考。

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機網(wǎng)站地址:jsp自定義標簽之ifelse與遍歷自定義標簽示例
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點

版權(quán)所有:易賢網(wǎng)