现在的位置: 首页 > 综合 > 正文

自定义标签之 SimpleTag的开发

2012年08月04日 ⁄ 综合 ⁄ 共 2922字 ⁄ 字号 评论关闭
先看一下SimpleTag的生命周期
1:每次遇到标签时,容器构造一个SimpleTage的实例,并且这个构造方法没有参数。和经典的标签是一样的,SimpleTag不能进行 缓冲,故不能重用,每次都需要构造新的实例。
2:调用民构造方法后,就用setJspContext()和setParent()方法,只有这个标签在另一个标签内时,才调用setParent()方法。
3:容器调用每个属性的setter方法以设置这些属性的值。
4:如果存在Body,那么就使用setJspBody方法设置这个标签的标签体。
5:容器调用doTag方法,所有的标签的逻辑,迭代和Body计算都在这个方法中。
6:当doTag方法返回时,所有参数都被锁定。
HelloWorld
还是从HelloWorld开始,标签需要实现SimpleTag接口。

package eflylab;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;

public class HelloWorldSimpleTag extends SimpleTagSupport {
    
    
public void doTag() throws JspException, IOException 
    
{
        getJspContext().getOut().write( 
"<table border=1><tr bgcolor=9944cc><td>simpeltag测试</tr></td><tr tr=cc44cc><td>helloWorld!</td></tr></table>" );
    }

}

可以看出,这个标签的作用就是输出一个表格,表格中间带有HelloWorld的字样。
下面是描述文件中的声明

 <tag>
    
<description>Outputs Hello, World</description>
        
<name>helloWorld</name>
    
<tag-class>eflylab.HelloWorldSimpleTag</tag-class>
    
<body-content>empty</body-content>
</tag>

测试代码

<%@ page contentType="text/html; charset=gb2312" language="java" %>
<%@ taglib uri="/demotag" prefix="mt"%> 
<html>
  
<head>
    
<title>JSP 2.0 Examples - 简单的标签</title>
  
</head>
  
<body>
    
<h1>JSP 2.0 Examples - 简单的标签</h1>
    
<hr>
    
<p>这里是一个非常简单的标签.</p>
    
<br>
    
<b><u>Result:</u></b>
    
<mt:helloWorld/>
  
</body>
</html>

运行如下:
 

实例开发:
上面是简单的HelloWorld,下面写一个简单的实例这个标签直接操作文件系统,它的使用就像是<img src=""/>HTML标签一样方便。

package eflylab;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.*;

public class FileTag extends SimpleTagSupport {
    
private String fileName;
    
public void setFileName(String fileName)
    
{
        
this.fileName=fileName;
    }

    
public void doTag() throws JspException, IOException
    
{
        BufferedReader in
=new BufferedReader(new FileReader(fileName));
        String temp
="";
        String fileContent
="";
        
while((temp=in.readLine())!=null)
        fileContent
+=temp;
        in.close();
        getJspContext().getOut().write( 
"<table border=1><tr bgcolor=9944cc><td>文件标签</tr></td><tr bgcolor=cc44cc><td>"+fileContent+"</td></tr></table>" );
    }

}

FileTag和HelloWorldTag之类的标签其实没有什么大的区别,只是它的业务逻辑不同而已。FileTag直接操作文件系统,并且从文件系统中读取内容显示到table中。和经典的标签一样,SimpleTag也可以有属性。FileTag有一个FileName的属性,这个属性用于表示文件的路径和名字。
描述如下:

 <tag>    
        
<name>file</name>
        
<tag-class>eflylab.FileTag</tag-class>
        
<body-content>empty</body-content>
        
<attribute>
              
<name>fileName</name>
              
<required>true</required>
              
<rtexprvalue>true</rtexprvalue>
          
</attribute>
     
</tag> 

测试代码:

<%@ page contentType="text/html; charset=gb2312" language="java" %>
<%@ taglib uri="/demotag" prefix="mt"%> 
<html>
  
<head>
    
<title>JSP 2.0 Examples - 文件标签</title>
  
</head>
  
<body>
    
<h1>JSP 2.0 Examples - 文件标签</h1>
    
<hr>
    
<p>这个标签可以直接使用文件.</p>
    
<br>
    
<b><u>Result:</u></b>
    
<mt:file fileName="c:\\MyService.log"/>
  
</body>
</html>

运行 :

抱歉!评论已关闭.