实现if elseif else的jsp标签。

本文介绍了一种在JSP中实现自定义if-elseif-else标签的方法,通过继承BodyTagSupport并利用bodyContent实现了更简洁的条件判断逻辑。文中提供了IfTag、ElseIfTag和ElseTag三个类的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 相信很多使用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。直接贴出所有代码,有兴趣的自己看看。  

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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;
    }
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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;
    }
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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.cnblogs.com/BearsTaR/archive/2010/07/29/jsp_if_elseif_else_tag.html   ,如需转载请自行联系原作者




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值