⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mytagforeach.java

📁 JSP自定义标签的实例 技术平台:Tomcat 5.0服务器; 开发工具:Eclipse+Lomboz、EditPlus开发工具; 操作系统:Windows XP Professional操作系
💻 JAVA
字号:
package cn.tag;
import java.io.IOException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class MytagforEach extends BodyTagSupport{
	private static final long serialVersionUID = 1L;
	protected String value = null; 		//对应forEach标签中的value属性
	protected int count = 0; 			//对应forEach标签中的count属性,默认为0
	public MytagforEach(){
		super();
	}
	public void setValue(String value){  //设置value属性值,容器会自动调用
		this.value = value;
	}
	public String getValue(){
		return value;
	}
	public void setCount(int count){		//设置count属性值,容器会自动调用
		this.count = count;
	}
	public int getCount(){
		return count;
	}	
	/***** 覆盖父类实现的doStartTag()方法 *****/
	public int doStartTag() throws JspTagException{
		if(count > 0){							//如果循环没有结束
			return EVAL_BODY_INCLUDE;		//表示继续进行体的处理
		}
		else{									//循环结束
			return SKIP_BODY;
		}		
	}
	/***** 覆盖父类实现的doEndTag()方法 *****/
    public int doEndTag() throws JspTagException{
		return EVAL_PAGE;
    }
	/***** 覆盖父类实现的doAfterBody()方法 *****/
	public int doAfterBody() throws JspTagException{
		if(count-- >= 1){							//循环一次,count减一,如果count还大于1,执行
			try{
				JspWriter out = bodyContent.getEnclosingWriter();
				out.println(bodyContent.getString()+value);
				out.println("<br>");
				bodyContent.clearBody();  		//清空bodyContent对象中的内容
			}catch(IOException e){
				System.out.println("Error in RepeatTag: "+e);
			}
			return EVAL_BODY_INCLUDE;
		}else{
			return SKIP_BODY;
		}
	}
	/***** 覆盖父类实现的doInitBody()方法 *****/
	public void doInitBody() throws JspTagException{
		//这里不作任何初始化
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -