org.apache.jasper.JasperException:找不到标记处理程序的属性表的setter方法

时间:2016-11-09 17:13:14

标签: java jsp jsp-tags

我在Jsp Custom标签中遇到问题。 我的TagHandler类PrintSqlTable定义了数据域名称或TLD标签内容是Same declare.but Complier显示异常: 找不到标记处理程序' table' 的属性的setter方法

标记处理程序类:

public class PrintSqlTable extends TagSupport {

private String id;
private String table;
private Connection dbConnection;

/**Set the id value
 * @param id*/
public void setId(String id) {
    this.id = id;
}

/**Set the table name
 * @param table*/
public void SetTable(String table) {
    this.table = table;
}

@Override
public int doStartTag() throws JspException {
    JspWriter out = pageContext.getOut();

    /**Establish the Database Connection*/
    try {
        dbConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jmysql",
                "root", "root!@#$Mysql2016");
        PreparedStatement pst = dbConnection.prepareStatement(
        "SELECT *FROM " + table + " WHERE id = ?");
        pst.setInt(1, Integer.parseInt(id));
        ResultSet rs = pst.executeQuery();

        if (rs != null) {
            ResultSetMetaData rsmd = rs.getMetaData();

            /**Get the Table Column Name*/
            out.println("<table>");
            out.println("<tr>");
            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                out.println("<th>" + rsmd.getColumnName(i) + "</th>");
            }
            out.println("</tr>");

            /**Get the Table Row Values*/
            if (rs.next()) {
                out.println("<tr>");
                for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                    out.println("<td>" + rs.getString(i) + "</td>");
                }
                out.println("</tr>");
            }
            out.println("</table>");

        } else {
            out.println(table + " Table Does not Found...!");
        }

    } catch (SQLException | IOException iosql) {
        System.out.println(iosql);
    }
    return SKIP_BODY;
}

}

TLD文件:

<?xml version="1.0" encoding="UTF-8"?>
<taglib >
<tlib-version>1.0</tlib-version>
<short-name>pt</short-name>
<uri>/WEB-INF/tlds/PrintTable</uri>
<tag>
<name>printRecored</name>
  <tag-class> com.jservlet.jsp.chapter.customtags.PrintSqlTable</tag-class>
<attribute>
   <name>id</name>
   <required>true</required>
</attribute>
<attribute>
      <name>table</name>
      <required>true</required>
  </attribute>
</tag>
</taglib>

JSP文件

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/tlds/PrintTable.tld" prefix="pt" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Print Table</title>
</head>
<body>
<pt:printRecored id="49" table="student"> </pt:printRecored>
</body>

1 个答案:

答案 0 :(得分:0)

您正在使用&#39; S &#39;即,SetTable(String table)方法(在您的PrintSqlTable类中)不遵循 Java Bean约定,因此需要更改方法名称,如下所示:

public void setTable(String table) {
    this.table = table;
}
  

对于每个标记属性,您必须在标记中定义set方法   符合JavaBeans体系结构约定的处理程序。   您可以查看here