JSP定制标记中的属性

对于任何自定义标签, 可能定义了太多属性。要定义属性, 你需要执行两个任务:

  • 使用属性名称在TagHandler类中定义属性并定义setter方法
  • 在TLD文件中的tag元素内定义attribute元素
让我们通过下面给出的标签来了解属性:
< m:cube number="4"> < /m:cube>

这里m是前缀, cube是标签名称, number是属性。
JSP自定义标记中属性的简单示例
在此示例中, 我们将使用多维数据集标记, 该标记返回任何给定数字的多维数据集。在这里, 我们为多维数据集标记定义了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 com.srcmini.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> com.srcmini.taghandler.CubeNumber< /tag-class> < attribute> < name> number< /name> < required> true< /required> < /attribute> < /tag> < /taglib>

输出量
Cube of 4 is: 64

数据库的JSP自定义标签属性示例 让我们创建一个自定义标签, 该标签针对给定的表名和ID打印表的特定记录。
因此, 你必须在标记处理程序类中具有两个属性。
PrintRecord.java
package com.srcmini; 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 valueif(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> srcmini< /uri> < tag> < name> printRecord< /name> < tag-class> com.srcmini.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="srcmini" prefix="j" %> < j:printRecord table="user874" id="1"> < /j:printRecord>

输出量 JSP定制标记中的属性

    推荐阅读