<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 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    實現if elseif else的jsp標簽。

    Posted on 2010-07-28 16:13 BearRui(AK-47) 閱讀(4247) 評論(29)  編輯  收藏 所屬分類: Java

        相信很多使用jstl的朋友都抱怨過,為什么jstl只有<c:if 而沒有elseif、else。當需要判斷多個條件的時候,只能寫多個<c:if 或者使用<c:choose。
    雖然struts有elseif 和 else標簽,不過看著就跟多個<c:if 沒什么2樣,使用如下:

    <s:if test="">  

    1

    </s:if>

    <s:elseif test="">

      2

    </s:elseif>

    <s:else>

       3

    </s:else>

    下面是本人實現的if elseif else。先看看使用代碼:

    <g:if test="">

       1

    <g:elseif test="" /> 

      2

    <g:else /> 

      3

    </g:if>

          這樣代碼結構個人覺得更加清晰簡單,類似freemarker的if elseif。

    實現:

    要實現上面說的if elseif,需要繼承BodyTagSupport,利用BodyTagSupport的bodyContent的來實現該功能,這里不具體介紹如何實現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;        //    用于存放成功條件后的內容
        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;
        }
        
    /**
         * 是否已經執行完畢
         * 
    @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()){
                
    // 已經有執行成功的條件,保存之前的html
                ifTag.setBody();
            }
    else if(test){        // 當前條件為true,之前無條件為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()){
                
    // 已經有執行成功的條件,保存之前的html
                ifTag.setBody();
            }
    else{
                
    // 之前沒有的判斷沒有成功條件,則清除之前的輸出
                ifTag.getBodyContent().clearBody();
                ifTag.succeeded();
            }
                
            
    return EVAL_BODY_BUFFERED;
        }
        
    }


    tld配置就不貼出來了,因為這個太簡單了,大家都知道的。

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

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

    評論

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。[未登錄]  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    # re: 實現if elseif else的jsp標簽。  回復  更多評論   

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

    謝謝支持,^_^
    主站蜘蛛池模板: 日韩精品亚洲人成在线观看| 免费a级毛片大学生免费观看 | 老司机福利在线免费观看| 日本zzzzwww大片免费| 狠狠色伊人亚洲综合成人| a在线免费观看视频| 西西人体大胆免费视频| 日韩人妻无码免费视频一区二区三区| 精品国产亚洲男女在线线电影 | 中文字幕看片在线a免费| 亚洲精品美女久久久久99小说| 最新亚洲人成网站在线观看 | 人妻在线日韩免费视频| 亚洲日韩中文字幕在线播放| 免费无码作爱视频| 91亚洲精品第一综合不卡播放| **俄罗斯毛片免费| 亚洲色大成网站www| 国产中文字幕免费| 国产免费人成视频在线播放播| 国产精品亚洲片在线观看不卡 | 亚洲乱色伦图片区小说| 亚洲?v无码国产在丝袜线观看| h片在线播放免费高清| 亚洲AV成人一区二区三区AV| 最近的中文字幕大全免费8| 精品亚洲456在线播放| 免费欧洲美女牲交视频| 好紧我太爽了视频免费国产| 亚洲视频免费在线看| 日韩在线a视频免费播放| 一级毛片大全免费播放| 久久亚洲中文字幕精品有坂深雪 | 精品久久8x国产免费观看| 亚洲欧美精品午睡沙发| 久久精品亚洲男人的天堂| 69免费视频大片| 美女视频免费看一区二区| 亚洲AV成人精品网站在线播放| 成年女人免费碰碰视频| 黄网站免费在线观看|