自定义标签 (choose)
生活随笔
收集整理的這篇文章主要介紹了
自定义标签 (choose)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
因為有3個標簽,所以寫3個標簽處理器類
1.ChooseTag
public class ChooseTag extends SimpleTagSupport {//定義一個標記,用于控制跳轉去向.public Boolean tag = true;public Boolean getTag() {return tag;}public void setTag(Boolean tag) {this.tag = tag;}@Overridepublic void doTag() throws JspException, IOException {
//獲取標簽體JspFragment jspBody = this.getJspBody();
//因為第一個是父標簽,所以直接執行就好了,不用處理.jspBody.invoke(null);super.doTag();}}
2.WhenTag
public class WhenTag extends SimpleTagSupport {//用來接收判斷條件public Boolean test;public Boolean getTest() {return test;}public void setTest(Boolean test) {this.test = test;}@Overridepublic void doTag() throws JspException, IOException {
//獲取父標簽,并且獲取他的tag的值ChooseTag chooseTag = (ChooseTag) this.getParent();Boolean tag = chooseTag.getTag();
//判斷tag的值.如果test也成立,就執行if (tag && this.getTest()) {chooseTag.setTag(false);
//獲取標簽體JspFragment jspBody = this.getJspBody();
//執行標簽體jspBody.invoke(null);}super.doTag();} }
3.OtherwiseTag
public class OtherwiseTag extends SimpleTagSupport{@Overridepublic void doTag() throws JspException, IOException {ChooseTag chooseTag = (ChooseTag) this.getParent();Boolean tag = chooseTag.getTag();//如果tag為true則說明沒有進whenif(tag){JspFragment jspBody = this.getJspBody();jspBody.invoke(null);}super.doTag();}}
?
寫tld文件
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> <taglib><tlibversion>1.2</tlibversion><jspversion>1.1</jspversion><shortname>mylib</shortname><uri>http://www.fz.song.myLib</uri><tag><name>choose</name><tagclass>fz.song.tag.ifelse.ChooseTag</tagclass><bodycontent>scriptless</bodycontent></tag><tag><name>when</name><tagclass>fz.song.tag.ifelse.WhenTag</tagclass><bodycontent>scriptless</bodycontent><attribute>
<!--屬性名--><name>test</name>
<!--是否必須--><required>true</required>
<!--是否使用el--><rtexprvalue>true</rtexprvalue></attribute></tag><tag><name>otherwise</name><tagclass>fz.song.tag.ifelse.OtherwiseTag</tagclass><bodycontent>scriptless</bodycontent></tag><tag><name>foreach</name><tagclass>fz.song.tag.foreach.ForeachTag</tagclass><bodycontent>scriptless</bodycontent><attribute><name>items</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>var</name><required>true</required><rtexprvalue>false</rtexprvalue></attribute></tag></taglib>
?
?
jsp頁面
<%@ taglib uri="http://www.fz.song.myLib" prefix="x"%><x:choose><x:when test="${10>1}">true</x:when><x:otherwise>false</x:otherwise></x:choose>?
補充:
<body-content></body-content>中可以寫入的參數有四種:
① empty
即標記體為空
② scriptless
這個標記不能有腳本元素,但可以有模板文本和EL, 還可以是定制和標準動作
③ tagdependent
標記體要看做是純文本,所以不會計算EL,也不會出發標記/動作
④ JSP
能放在JSP中的東西都能放在這個標記體中
轉載于:https://www.cnblogs.com/songfahzun/p/4918925.html
總結
以上是生活随笔為你收集整理的自定义标签 (choose)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《FLUENT 14流场分析自学手册》—
- 下一篇: HDOJ1861 游船出租