📅  最后修改于: 2021-01-05 01:12:43             🧑  作者: Mango
对于任何自定义标签,可能定义了太多属性。要定义属性,您需要执行两个任务:
让我们通过下面给出的标签来了解属性:
这里m是前缀, cube是标签名称, number是属性。
在此示例中,我们将使用多维数据集标记,该标记返回任何给定数字的多维数据集。在这里,我们定义了多维数据集标记的number属性。我们在这里使用三个文件:
<%@ taglib uri="WEB-INF/mytags.tld" prefix="m" %>
Cube of 4 is:
package com.javatpoint.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;
}
}
1.0
1.2
simple
http://tomcat.apache.org/example-taglib
A simple tab library for the examples
cube
com.javatpoint.taghandler.CubeNumber
number
true
- Cube of 4 is: 64
让我们创建一个自定义标记,为给定的表名和ID打印表的特定记录。
因此,您必须在标记处理程序类中具有两个属性。
package com.javatpoint;
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("");
out.write("");
for(int i=1;i<=totalcols;i++){
out.write(""+rsmd.getColumnName(i)+" ");
}
out.write(" ");
//column value
if(rs.next()){
out.write("");
for(int i=1;i<=totalcols;i++){
out.write(""+rs.getString(i)+" ");
}
out.write(" ");
}else{
out.write("Table or Id doesn't exist");
}
out.write("
");
}
con.close();
}catch(Exception e){System.out.println(e);}
return SKIP_BODY;
}
}
1.2
2.0
c
javatpoint
printRecord
com.javatpoint.PrintRecord
id
true
table
true
<%@ taglib uri="javatpoint" prefix="j" %>