JSLT自定义标签

来源:互联网 发布:葡萄牙帝国知乎 编辑:程序博客网 时间:2024/05/21 14:05

http://java.sun.com/developer/technicalArticles/xml/WebAppDev3/index.html

1  tld文件的写法
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems,Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<!-- a tag library descriptor -->
<taglib>
   
<tlibversion>1.0</tlibversion>
   
<jspversion>1.1</jspversion>
   
<shortname>first</shortname>
   
<uri></uri>
   
<info>A simple tab library for the examples</info>
 
  
<tag>
  
<name>helloparam</name>
  
<tagclass>mytags.HelloTagParam</tagclass>
  
<bodycontent>empty</bodycontent>
  
<info>Tag with Parameter</info>
  
<attribute>
     
<name>name</name>
     
<required>false</required>
     
<rtexprvalue>false</rtexprvalue>
  
</attribute>
  
<attribute>
     
<name>pwd</name>
     
<required>false</required>
     
<rtexprvalue>false</rtexprvalue>
  
</attribute>
  
</tag>
</taglib>

2  标签处理类的写法

package mytags;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class HelloTagParam extends TagSupport {
   
private String name;
   
private String pwd;
  
   
public void setName(String name) {
      
this.name = name;
   }

  
   
public void setPwd(String pwd) {
       
this.pwd = pwd;
    }
  
   
public int doStartTag() throws JspException {
      
try {
         pageContext.getOut().print(
"Welcome, " +name+"  "+pwd);
      }
 catch (IOException ioe) {
         
throw new JspException("Error: IOException while writing to client");
      }

      
return SKIP_BODY;
   }

   
public int doEndTag() throws JspException {
      
return SKIP_PAGE;
   }

}

3  在JSP页面上的应用

<%@ taglib uri="/WEB-INF/taglib.tld" prefix="first" %> 
<first:helloparam name="中国人" pwd="1984"/>
原创粉丝点击