jsp自定義標簽之ifelse與遍歷自定義標簽示例
更新時間:2014年03月24日 16:56:22 作者:
這篇文章主要介紹了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)成大寫的代碼,其他的都一致;
關于if else的代碼,太多,我放到資源里了,有必要的話可以下載下來,僅供參考。
簡單的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)成大寫的代碼,其他的都一致;
關于if else的代碼,太多,我放到資源里了,有必要的話可以下載下來,僅供參考。
您可能感興趣的文章:
- javascript 從if else 到 switch case 再到抽象
- JS中三目運算符和if else的區(qū)別分析與示例
- js利用與或運算符優(yōu)先級實現(xiàn)if else條件判斷表達式
- vue.js 使用v-if v-else發(fā)現(xiàn)沒有執(zhí)行解決辦法
- JavaScript If...Else 聲明
- 詳解JavaScript 中 if / if...else...替換方式
- js 中的switch表達式使用示例
- JavaScript switch case 的用法實例[范圍]
- JavaScript中switch判斷容易犯錯的一個細節(jié)
- 其實你可以少寫點if else與switch(推薦)
相關文章
(jsp/html)網(wǎng)頁上嵌入播放器(常用播放器代碼整理)
網(wǎng)頁上嵌入播放器,只要在HTML上添加以上代碼就OK了,下面整理了一些常用的播放器代碼,總有一款適合你,感興趣的朋友可以參考下哈,希望對你有所幫助2013-05-05
jsp response.sendRedirect()用法詳解
這篇文章主要介紹了jsp response.sendRedirect()用法詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08

