打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
自定义if elseif else的jsp标签

相信很多使用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配置就不贴出来了,因为这个太简单了,大家都知道的。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
原来C语言也可以面向对象(一)
activiti源码学习之ProcessEngine
Android 中的工厂模式(减少 if-else 和 switch-case)
java的debug和release编译方式
Java中Condition的使用
java之Synchronized(锁住对象和锁住代码)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服