JSP自定义标签-属性

1 带属性的JSP自定义标签

对于任何JSP自定义标签,可能都需要定义属性。要定义属性,您需要执行两个任务:

  • 使用属性名称在TagHandler类中定义属性并定义setter方法
  • 在TLD文件中的tag元素内定义attribute元素

让我们通过下面给出的标签来了解属性:

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

上面的m是前缀,show是标签名称,number是属性。

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

在下面的示例中,我们将定义一个标签,返回任何给定数字的立方数。我们需要为标签定义了number属性。具体看下面:

2.1 编写标签处理程序

CubeNumber:

package com.yiidian;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.util.Calendar;

/**
 * 一点教程网 - http://www.yiidian.com
 */
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;
    }
}

2.2 编写mytags.tld文件

mytags.tld:

<?xml version="1.0" encoding="utf-8" ?>
<!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://www.yiidian.com/example-taglib</uri>
    <description>A simple tab library for the examples</description>

    <tag>
        <name>cube</name>
        <tag-class>com.yiidian.CubeNumber</tag-class>
        <attribute>
            <name>number</name>
            <required>true</required>
        </attribute>
    </tag>

</taglib>

2.3 编写index.jsp

index.jsp:

<%@ page language="java" contentType="text/html;charset=utf-8" %>
<%@ taglib uri="WEB-INF/mytags.tld" prefix="m" %>
<html>
<html>
<head>
    <meta charset="utf-8">
    <title>一点教程网-带属性的JSP自定义标签</title>
</head>
<body>

4的立方为: <m:cube number="4"></m:cube>

</body>
</html>

2.4 运行测试

3 带属性的JSP自定义标签遍历数据库记录

接下来,我们创建一个自定义标签,输出数据库中特定表和ID的记录。(传入表名和ID号作为属性)

3.1 创建表

为了更好测试,在MySQL的test数据库建立customer表,SQL语句如下:

CREATE TABLE `customer` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `name` varchar(50) DEFAULT NULL,
   `gender` varchar(20) DEFAULT NULL,
   `telephone` varchar(100) DEFAULT NULL,
   `address` varchar(100) DEFAULT NULL,
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8

3.2 编写标签处理程序

PrintRecord:

package com.yiidian;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.sql.*;
import java.util.Calendar;

/**
 * 一点教程网 - http://www.yiidian.com
 */
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("com.mysql.jdbc.Driver");
            Connection con= DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test","root","root");
            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("表或ID不存在");
                }
                out.write("</table>");

            }
            con.close();
        }catch(Exception e){System.out.println(e);}
        return SKIP_BODY;
    }
}

3.3 编写mytags.tld文件

mytags.tld:

<?xml version="1.0" encoding="utf-8" ?>
<!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>yiidian.com</uri>

    <tag>
        <name>printRecord</name>
        <tag-class>com.yiidian.PrintRecord</tag-class>
        <attribute>
            <name>id</name>
            <required>true</required>
        </attribute>
        <attribute>
            <name>table</name>
            <required>true</required>
        </attribute>

    </tag>
</taglib>

3.4 编写index.jsp

index.jsp:

<%@ page language="java" contentType="text/html;charset=utf-8" %>
<%@ taglib uri="yiidian.com" prefix="y" %>
<html>
<html>
<head>
    <meta charset="utf-8">
    <title>一点教程网-带属性的JSP自定义标签输出数据库记录</title>
</head>
<body>

<y:printRecord table="customer" id="1"></y:printRecord>

</body>
</html>

3.5 运行测试

 

热门文章

优秀文章