在任何自定义标签中,可以定义许多属性。要定义属性,需要执行两个任务:

  • 在TagHandler类中用属性名定义属性,并定义setter方法
  • 在TLD文件中的标签元素内定义属性元素

让我们通过下面的标签来理解属性:

<m:cube number="4"></m:cube>  

这里 m 是前缀,cube 是标签名,number 是属性。

JSP自定义标签中属性的简单示例

在此示例中,我们将使用cube标签返回任何给定数字的立方。这里,我们为cube标签定义了number属性。我们在这里使用了三个文件:

  • index.jsp
  • CubeNumber.java
  • mytags.tld

index.jsp

<%@ taglib uri="WEB-INF/mytags.tld" prefix="m" %>  
Cube of 4 is: <m:cube number="4"></m:cube>  

CubeNumber.java

package cn.javatiku.taghandler;  
import javax.servlet.jsp.JspException;  
import javax.servlet.jsp.JspWriter;  
import javax.servlet.jsp.tagext.TagSupport;  
  
public class CubeNumber extends TagSupport{  
private int number;  
      
public void setNumber(int number) {  
    this.number = number;  
}  
  
public int doStartTag() throws JspException {  
    JspWriter out=pageContext.getOut();  
    try{  
        out.print(number*number*number);  
    }catch(Exception e){e.printStackTrace();}  
      
    return SKIP_BODY;  
}  
}  

mytags.tld

<?xml version="1.0" encoding="ISO-8859-1" ?>  
<!DOCTYPE taglib  
        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"  
        "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">  
  
<taglib>  
  <tlib-version>1.0</tlib-version>  
  <jsp-version>1.2</jsp-version>  
  <short-name>simple</short-name>  
  <uri>http://tomcat.apache.org/example-taglib</uri>  
  <description>A simple tab library for the examples</description>  
  
  <tag>  
    <name>cube</name>  
    <tag-class>cn.javatiku.taghandler.CubeNumber</tag-class>  
    <attribute>  
    <name>number</name>  
    <required>true</required>  
    </attribute>  
  </tag>  
</taglib>  

输出

Cube of 4 is: 64  

使用数据库的JSP自定义标签属性示例

让我们创建一个自定义标签,它打印给定表名和ID的特定表记录。

因此,您必须在标签处理类中有两个属性。

PrintRecord.java

package cn.javatiku;  
import javax.servlet.jsp.JspException;  
import javax.servlet.jsp.JspWriter;  
import javax.servlet.jsp.tagext.TagSupport;  
import java.sql.*;  
  
public class PrintRecord extends TagSupport{  
private String id;  
private String table;  
  
public void setId(String id) {  
    this.id = id;  
}  
public void setTable(String table) {  
    this.table = table;  
}  
  
public int doStartTag()throws JspException{  
    JspWriter out=pageContext.getOut();  
    try{   
        Class.forName("oracle.jdbc.driver.OracleDriver");  
        Connection con=DriverManager.getConnection(  
                 "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
        PreparedStatement ps=con.prepareStatement("select * from "+table+" where id=?");  
        ps.setInt(1,Integer.parseInt(id));  
        ResultSet rs=ps.executeQuery();  
        if(rs!=null){  
        ResultSetMetaData rsmd=rs.getMetaData();  
        int totalcols=rsmd.getColumnCount();  
        //column name  
        out.write("<table border='1'>");  
        out.write("<tr>");  
        for(int i=1;i<=totalcols;i++){  
            out.write("<th>"+rsmd.getColumnName(i)+"</th>");  
        }  
        out.write("</tr>");  
        //column value  
          
        if(rs.next()){  
            out.write("<tr>");  
                for(int i=1;i<=totalcols;i++){  
                out.write("<td>"+rs.getString(i)+"</td>");  
            }  
            out.write("</tr>");  
                  
        }else{  
            out.write("Table or Id doesn't exist");  
        }  
        out.write("</table>");  
          
        }  
        con.close();  
    }catch(Exception e){System.out.println(e);}  
    return SKIP_BODY;  
}  
}  

m.tld

<?xml version="1.0" encoding="ISO-8859-1" ?>    
<!DOCTYPE taglib    
        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"    
    "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">    
    
<taglib>  
  
  <tlib-version>1.2</tlib-version>  
  <jsp-version>2.0</jsp-version>  
  <short-name>c</short-name>  
  <uri>javatiku</uri>  
  
<tag>  
<name>printRecord</name>  
<tag-class>cn.javatiku.PrintRecord</tag-class>  
<attribute>  
<name>id</name>  
<required>true</required>  
</attribute>  
<attribute>  
<name>table</name>  
<required>true</required>  
</attribute>  
  
</tag>  
</taglib>

index.jsp

<%@ taglib uri="javatiku" prefix="j" %>  
<j:printRecord table="user874" id="1"></j:printRecord>  

输出

5-1.png

标签: JSP, JSP教程, JSP技术, JSP快速入门, JSP简单用法, JSP编程, JSP下载, JSP基本语法, JSP安装教程, JSP库, JSP指南, JSP基础教程, JSP初级教程, JSP进阶教程