<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 36, comments - 419, trackbacks - 0, articles - 0
      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。

    Posted on 2010-07-28 16:13 BearRui(AK-47) 閱讀(4251) 評(píng)論(29)  編輯  收藏 所屬分類(lèi): Java

        相信很多使用jstl的朋友都抱怨過(guò),為什么jstl只有<c:if 而沒(méi)有elseif、else。當(dāng)需要判斷多個(gè)條件的時(shí)候,只能寫(xiě)多個(gè)<c:if 或者使用<c:choose。
    雖然struts有elseif 和 else標(biāo)簽,不過(guò)看著就跟多個(gè)<c:if 沒(méi)什么2樣,使用如下:

    <s:if test="">  

    1

    </s:if>

    <s:elseif test="">

      2

    </s:elseif>

    <s:else>

       3

    </s:else>

    下面是本人實(shí)現(xiàn)的if elseif else。先看看使用代碼:

    <g:if test="">

       1

    <g:elseif test="" /> 

      2

    <g:else /> 

      3

    </g:if>

          這樣代碼結(jié)構(gòu)個(gè)人覺(jué)得更加清晰簡(jiǎn)單,類(lèi)似freemarker的if elseif。

    實(shí)現(xiàn):

    要實(shí)現(xiàn)上面說(shuō)的if elseif,需要繼承BodyTagSupport,利用BodyTagSupport的bodyContent的來(lái)實(shí)現(xiàn)該功能,這里不具體介紹如何實(shí)現(xiàn)jsp tag。直接貼出所有代碼,有興趣的自己看看。 

    public class IfTag extends BodyTagSupport{

        
    public IfTag() {
            
    super();
            init();
        }

        @Override
        
    public void release() {
            
    super.release();
            init();
        }
        
        @Override
        
    public int doStartTag() throws JspException {
            
    if(test){
                
    this.succeeded();
            }
            
    return EVAL_BODY_BUFFERED;
        }

        @Override
        
    public int doEndTag() throws JspException {
            
    try {
                
    if(subtagSucceeded)
                    pageContext.getOut().write(getBody());
            } 
    catch (IOException e) {
                
    throw new JspException("IOError while writing the body: " + e.getMessage(), e);
            }
            
            init();
            
    return super.doEndTag();
        }
        
        
    private String body = null;        //    用于存放成功條件后的內(nèi)容
        public void setBody(){
            
    if(body == null){
                body 
    = bodyContent.getString().trim();
            }
        }
        
        
    private String getBody(){
            
    if(body == null)
                
    return bodyContent.getString().trim();
            
    else
                
    return body;
        }
        
        
    /**
         * 判斷if 或者 子 else if是否提交成功
         
    */
        
    private boolean subtagSucceeded;
        
        
    /**
         * 子條件判斷成功
         
    */
        
    public void succeeded(){
            subtagSucceeded 
    = true;
        }
        
    /**
         * 是否已經(jīng)執(zhí)行完畢
         * 
    @return
         
    */
        
    public boolean isSucceeded(){
            
    return subtagSucceeded;
        }
        
        
    private void init() {
            test 
    = false;
            subtagSucceeded 
    = false;
            body 
    = null;
        }
        
        
    private boolean test;  
        
        
    public void setTest(boolean test) {
            
    this.test = test;
        }
    }

     

     


    public class ElseIfTag extends BodyTagSupport{

        
    public ElseIfTag() {
            
    super();
            init();
        }

        @Override
        
    public int doStartTag() throws JspException {
            Tag parent 
    = getParent();

            
    if(parent==null || !(parent instanceof IfTag)){
                
    throw new JspTagException("else tag must inside if tag");
            }
            
            IfTag ifTag 
    = (IfTag)parent;
            
    if(ifTag.isSucceeded()){
                
    // 已經(jīng)有執(zhí)行成功的條件,保存之前的html
                ifTag.setBody();
            }
    else if(test){        // 當(dāng)前條件為true,之前無(wú)條件為true
                ifTag.succeeded();
                
    // 則清除之前的輸出
                ifTag.getBodyContent().clearBody();
            }
                
            
    return EVAL_BODY_BUFFERED;
        }
         
        @Override
        
    public void release() {
            
    super.release();
            init();
        }
        
        
    private void init() {
            test 
    = false;
        }
        
        
    private boolean test;  
        
        
    public void setTest(boolean test) {
            
    this.test = test;
        }
    }

     
    public class ElseTag extends BodyTagSupport{

        
    public void release() {
            
    super.release();
        }
        
        
    public int doStartTag() throws JspException {
            Tag parent 
    = getParent();

            
    if(parent==null || !(parent instanceof IfTag)){
                
    throw new JspTagException("else tag must inside if tag");
            }
            
            IfTag ifTag 
    = (IfTag)parent;
            
    if(ifTag.isSucceeded()){
                
    // 已經(jīng)有執(zhí)行成功的條件,保存之前的html
                ifTag.setBody();
            }
    else{
                
    // 之前沒(méi)有的判斷沒(méi)有成功條件,則清除之前的輸出
                ifTag.getBodyContent().clearBody();
                ifTag.succeeded();
            }
                
            
    return EVAL_BODY_BUFFERED;
        }
        
    }


    tld配置就不貼出來(lái)了,因?yàn)檫@個(gè)太簡(jiǎn)單了,大家都知道的。

    [作者]:BearRui(AK-47)
    [博客]: http://www.tkk7.com/bearrui/
    [聲明]:本博所有文章版權(quán)歸作者所有(除特殊說(shuō)明以外),轉(zhuǎn)載請(qǐng)注明出處.
    英雄,別走啊,幫哥評(píng)論下:  

    精彩推薦 好文要頂 水平一般 看不懂 還需努力

    評(píng)論

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:10 by popo4j
    好文章,頂一下!

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:10 by popo4j
    文章很精彩,推薦一下!

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:10 by popo4j
    好文章,頂一下!

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:10 by popo4j
    文章寫(xiě)的一般般,:)

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:10 by popo4j
    看不懂哦,~_~

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:10 by popo4j
    博主還需努力啊!

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:11 by popo4j
    看不懂哦,~_~

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:11 by popo4j
    文章寫(xiě)的一般般,:)

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:11 by popo4j
    好文章,頂一下!

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:11 by popo4j
    文章很精彩,推薦一下!

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:11 by popo4j
    好文章,頂一下!

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:11 by popo4j
    文章寫(xiě)的一般般,:)

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:11 by popo4j
    看不懂哦,~_~

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:11 by popo4j
    博主還需努力啊!

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:11 by popo4j
    看不懂哦,~_~

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:11 by popo4j
    文章寫(xiě)的一般般,:)

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:11 by popo4j
    好文章,頂一下!

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:11 by popo4j
    文章很精彩,推薦一下!

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:11 by popo4j
    好文章,頂一下!

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:11 by popo4j
    文章寫(xiě)的一般般,:)

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:11 by popo4j
    看不懂哦,~_~

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:11 by popo4j
    博主還需努力啊!

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:11 by popo4j
    看不懂哦,~_~

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:11 by popo4j
    文章寫(xiě)的一般般,:)

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:11 by popo4j
    文章很精彩,推薦一下!

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-29 17:14 by BearRui(AK-47)
    暈,樓上的干嘛了。

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。[未登錄](méi)  回復(fù)  更多評(píng)論   

    2010-07-30 09:40 by alan
    ......

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-07-30 11:03 by 凡客
    文章很精彩

    # re: 實(shí)現(xiàn)if elseif else的jsp標(biāo)簽。  回復(fù)  更多評(píng)論   

    2010-08-01 22:43 by BearRui(AK-47)
    @凡客

    謝謝支持,^_^
    主站蜘蛛池模板: 亚洲精品偷拍视频免费观看| 国产亚洲人成在线播放| 亚洲欧洲日产国码在线观看| 亚洲女人影院想要爱| 亚洲精品无码久久久久秋霞| 美女羞羞视频免费网站| 丁香花在线观看免费观看图片 | 最近中文字幕mv免费高清视频7 | 亚洲欧洲视频在线观看| 亚洲色大情网站www| 一个人看的www免费在线视频| 免费国产在线视频| a毛片基地免费全部视频| 日韩亚洲国产综合久久久| 亚洲日本va在线视频观看| 亚洲乱码无限2021芒果| 免费视频成人国产精品网站 | 国产午夜成人免费看片无遮挡| 91精品成人免费国产片| 国产精品黄页在线播放免费| 曰韩亚洲av人人夜夜澡人人爽 | 久久精品国产亚洲AV忘忧草18 | 在线观看免费无码专区| 日韩免费一区二区三区在线| 免费在线观看黄网| 久久精品国产亚洲AV嫖农村妇女| 亚洲精品国产av成拍色拍| 亚洲精品WWW久久久久久| 久久久久亚洲AV片无码下载蜜桃| 亚洲愉拍一区二区三区| 怡红院免费的全部视频| 无码一区二区三区免费视频| 亚洲人成亚洲人成在线观看 | 特级aaaaaaaaa毛片免费视频| 久久香蕉国产线看免费| 免费无码成人AV片在线在线播放| 亚洲国产精品一区二区久久hs| 亚洲综合成人婷婷五月网址| a毛片视频免费观看影院| 国产真人无遮挡作爱免费视频 | 亚洲一区二区三区久久|