惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
雷峰网
雷峰网
Last Week in AI
Last Week in AI
T
Tailwind CSS Blog
V
Visual Studio Blog
Jina AI
Jina AI
博客园 - 司徒正美
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
小众软件
小众软件
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队
博客园 - 【当耐特】
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
WordPress大学
WordPress大学
爱范儿
爱范儿
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏

博客园 - BearRui(AK-47)

产生唯一随机码的方法分析。 URL中允许携带sessionid带来的安全隐患。 JS 实现完美include - BearRui(AK-47) - 博客园 基于模板的excel导出 记一复杂页面的前端优化(2) - 其他优化 - BearRui(AK-47) 记一复杂页面的前端优化(1) - 不一样的延迟加载 名站技术分析 — tudou网首页下列菜单的弹出效果 LESS 让css也支持变量,运算符,include,嵌套规则等等 CSS技巧 — 不使用图片实现圆角、阴影、渐变等功能 名站技术分析 — facebook奇特的页面加载技术 了解CSS的查找匹配原理,让CSS更简洁、高效 让ajax更加友好,实时显示后台处理进度。 - BearRui(AK-47) - 博客园 [翻译] IE7,IE8 BUG导致CSS下载二次。 - BearRui(AK-47) 名站技术分析 - 浅谈tudou.com首页图片延迟加载的效果 - BearRui(AK-47) 高性能WEB开发(11) - flush让页面分块,逐步呈现 WEB高性能开发(10) - 疯狂的HTML压缩 该如何加载google-analytics(或其他第三方)的JS web高性能开发系列随笔 管理好程序中的DLL - BearRui(AK-47) - 博客园
实现if elseif else的jsp标签。 - BearRui(AK-47)
BearRui(AK-4 · 2010-07-29 · via 博客园 - BearRui(AK-47)

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

posted on 2010-07-29 08:46  BearRui(AK-47)  阅读(18269)  评论()    收藏  举报