jsp自訂標籤實現java的switch……case執行個體

來源:互聯網
上載者:User

1、實現MySwitchTag類

package class3g.web.tag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class MySwitchTag extends SimpleTagSupport {private int exp;private boolean lastCase_break = false;  //上一個標籤的break狀態private boolean lastCase_done = false;  //上一個標籤的執行狀態private boolean terminate = false;public boolean isLastCase_break() {return lastCase_break;}public void setLastCase_break(boolean lastCase_break) {this.lastCase_break = lastCase_break;}public boolean isLastCase_done() {return lastCase_done;}public void setLastCase_done(boolean lastCase_done) {this.lastCase_done = lastCase_done;}public boolean isTerminate() {return terminate;}public void setTerminate(boolean terminate) {this.terminate = terminate;}public int getExp() {return exp;}public void setExp(int exp) {this.exp = exp;}@Overridepublic void doTag() throws JspException, IOException {this.getJspBody().invoke(null);}}

實現MyCaseTag類

package class3g.web.tag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class MyCaseTag extends SimpleTagSupport {private int value;private boolean breakSwitch;public void setValue(int value) {this.value = value;}public void setBreakSwitch(boolean breakSwitch) {this.breakSwitch = breakSwitch;}@Overridepublic void doTag() throws JspException, IOException {MySwitchTag parent = (MySwitchTag) this.getParent();if (parent.isTerminate() == false) {if (parent.isLastCase_done() == false) {if (parent.getExp() == value) {this.getJspBody().invoke(null);parent.setLastCase_done(true);parent.setTerminate(breakSwitch);}} else {if (parent.isLastCase_break() == false) {this.getJspBody().invoke(null);parent.setLastCase_done(true);parent.setLastCase_break(breakSwitch);parent.setTerminate(breakSwitch);}}}}}

實現MyDefaultTag類

package class3g.web.tag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;public class MyDefaultTag extends SimpleTagSupport {@Overridepublic void doTag() throws JspException, IOException {MySwitchTag parent =  (MySwitchTag) this.getParent();if(parent.isTerminate() == false){this.getJspBody().invoke(null);parent.setTerminate(true);}}}

2、修改tld檔案

<tag><description>switch tag</description>        <name>switch</name><tag-class>class3g.web.tag.MySwitchTag</tag-class><body-content>scriptless</body-content><attribute><name>exp</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute>    </tag>            <tag><description>case tag </description>        <name>case</name><tag-class>class3g.web.tag.MyCaseTag</tag-class><body-content>scriptless</body-content><attribute><name>value</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>breakSwitch</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute>    </tag>        <tag><description>default </description>        <name>default</name><tag-class>class3g.web.tag.MyDefaultTag</tag-class><body-content>scriptless</body-content>    </tag>

3、在jsp實用自訂標籤

 <g:switch exp="2">    <g:case value="1" breakSwitch="true" >    11111111111<br/>    </g:case>    <g:case value="2" breakSwitch="false" >    22222222222222<br/>    </g:case>    <g:case value="3" breakSwitch="true" >    333333333333333<br/>    </g:case>    <g:default>    dddddddddddddddddd<br/>    </g:default>        </g:switch>    <!--     switch:帶有一個exp屬性,三個成員變數    terminate作為中斷執行break的標誌    lastCase_Done:上一個case標籤的執行狀態    lastCase_break:上一個case標籤的break值狀態            case:兩個屬性,value和布爾型的breakSwitch,沒成員    default:沒屬性,沒成員     -->

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.