自定义标签入门3

 
使用自定义标签
 
一、控制标签体内容是否执行
  1.处理类
package cn.zengfansheng.simpletag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

//<simple:execute>标签
public class ExecuteTag extends SimpleTagSupport {
	public void doTag() throws JspException, IOException {
		//取得标签体中的内容,并封装成JspFragment对象
		JspFragment jspFragment = this.getJspBody();
		//将标签体的内容输出到浏览器,null表示浏览器
		jspFragment.invoke(null);
	}
}

  2.WEB-INF目录下,建立simple.tld文件

<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
	    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
	    version="2.0">
	  <tlib-version>1.0</tlib-version>
	  <short-name>simple</short-name>
	  <uri>http://www.zengfansheng.cn/jstl/simple</uri>
        <tag>
	  	<name>execute</name>
	  	<tag-class>cn.zengfansheng.simpletag.ExecuteTag</tag-class>
		<body-content>scriptless</body-content>	  
	</tag>
</taglib>    

  3.执行

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://www.zengfansheng.cn/jstl/simple"  prefix="simple"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
  	 <simple:execute>
  	 	执行了吗??
  	 </simple:execute>
  </body>
</html>
 
二、控制标签后内容是否执行
  1.处理类
public class ExecuteTag extends SimpleTagSupport {
	public void doTag() throws JspException, IOException {
		//取得标签体中的内容,并封装成JspFragment对象
		JspFragment jspFragment = this.getJspBody();
		//将标签体的内容输出到浏览器,null表示浏览器
		jspFragment.invoke(null);
		throw new SkipPageException();//控制<simple:execute><、simple:execute>标签后的内容是否执行
	}
}
 
三、控制标签体内容复重执行
  1.处理类,没有属性
public class For1tag extends SimpleTagSupport {
	public void doTag() throws JspException, IOException {
//			向浏览器输出十遍
			for(int i =0;i<10;i++)
				this.getJspBody().invoke(null);
	}
}

  2.带有属性的

    处理类:

public class For2tag extends SimpleTagSupport {
		//重复调用,带有参数
	int sum;//sum要和tld中的attribute-name值保持一致
	public void setSum(int sum) {
		this.sum = sum;
	}
	public void doTag() throws JspException, IOException {
			//向浏览器输出十遍
			for(int i =0;i<sum;i++)
				this.getJspBody().invoke(null);
	}
}

    simple.tld文件:

     <tag>
	  		<name>for2</name>
	  		<tag-class>cn.zengfansheng.simpletag.For2tag</tag-class>
	  		<body-content>scriptless</body-content>
	  		<attribute>
	  			<name>sum</name>
	  			<required>true</required>//决定属性是否需要
	  			<rtexprvalue>true</rtexprvalue>//决定是否可以使用el表达式,例如:<%=value %> 或者 ${value}
	  		</attribute>
     </tag>

    执行页面:

<simple:for2 sum="<%= 1000 %>">
  	 <input  type="checkbox"  checked="checked" />
</simple:for2>
 
四、控制标签体内容改变-将小写字母转成大写字母
    1.标签处理类
public class UpperTagDemo extends SimpleTagSupport {

//	将小写字母转成大写字母然后输出到浏览器中去
	public void doTag() throws JspException, IOException {

			//将"abcd"暂时存放在缓存中
			JspFragment jf = this.getJspBody();
			
			//定义一个字符串缓存
			StringWriter writer = new StringWriter();
			jf.invoke(writer);
			
			//从缓存中取得"abcd"
			String temp = writer.getBuffer().toString();
			
			//将缓存中的"abcd"转成"ABCD"
			temp  = temp.toUpperCase();

			//将"ABCD"输到浏览器
			JspWriter out = this.getJspContext().getOut();
			out.print(temp);
	}
}

     2.simple.tld

        <tag>
	  	<name>upper</name>
	  	<tag-class>cn.zengfansheng.simpletag.UpperTagDemo</tag-class>
		<body-content>scriptless</body-content>	  
	</tag>    

    3.执行

    <simple:upper>abcd</simple:upper>
 
五、控制标签体内容复重执行,复重次数由标签属性决定
    1.处理类:
//<simple:for3 var="num" begin="1" end="10" step="1(默认为1)">标签处理类
public class For3Tag extends SimpleTagSupport {
		
		private String var1;
		private int begin;
		private int end;
		private int step = 1;//若此属性没有,则默认为1
		
		public void setVar1(String var1) {
			System.out.println("setVar1");
			this.var1 = var1;
		}
		public void setBegin(int begin) {
			this.begin = begin;
		}
		public void setEnd(int end) {
			this.end = end;
		}
		public void setStep(int step) {
			this.step = step;
		}
		
		public void doTag() throws JspException, IOException {
			
			for(int i=this.begin;i<=this.end;i+=this.step){
				PageContext pageContext = (PageContext) this.getJspContext();
				pageContext.setAttribute(this.var1, i);
				this.getJspBody().invoke(null);
			}
		}
}

    2.simple.tld

       <tag>
	  	<name>for3</name>
	  	<tag-class>cn.zengfansheng.simpletag.For3Tag</tag-class>
	  	<body-content>scriptless</body-content>
	  	<attribute>
  			<name>var1</name>
  			<required>true</required>
  			<rtexprvalue>true</rtexprvalue>
  		</attribute>
  		<attribute>
  			<name>begin</name>
  			<required>true</required>
  			<rtexprvalue>true</rtexprvalue>
  		</attribute>
  		<attribute>
  			<name>end</name>
  			<required>true</required>
  			<rtexprvalue>true</rtexprvalue>
  		</attribute>
  		<attribute>
  			<name>step</name>
  			<required>false</required>
  			<rtexprvalue>true</rtexprvalue>
  		</attribute>
	  </tag>

    3.执行

        <simple:for3 end="120"   begin="5"  var1="temp" step="5">
  	 	${temp}
  	 </simple:for3>    

  

 
 
 
 
posted @ 2013-04-10 00:09  hacket520  阅读(151)  评论(0)    收藏  举报