从 JSP 和 servlet 开发开始,我在将要插入 JSP 页面的无主体自定义标记方面遇到了一些问题。
完成的步骤:
- 在目录中成功编写并编译了一个 CustomTag.java(扩展
TagSupport
) ;WEB-INF/classes
- 用一个非常简单的例子定义了 TLD 文件,包括一个无主体标签
<body-content>
的empty
值; - 在 JSP 页面中使用指向我的
/WEB-INF/tlds/site.tld
文件的 taglib 指令的标记。
考虑到所有这些,您是否知道为什么 Tomcat 会发送这样的错误:
CustomTag 无法解析为类型
提前感谢您的回答,如果您需要更多详细信息,请询问。
这是我的 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/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>customlib</short-name>
<description>Custom library.</description>
<tag>
<name>header</name>
<tag-class>HeaderTag</tag-class>
<body-content>empty</body-content>
<description>...</description>
</tag>
</taglib>
JSP 文件:
<%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage="" %>
<%@ taglib uri="/WEB-INF/tlds/customlib.tld" prefix="clib" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>title</title>
</head>
<body>
<clib:header />
</body>
</html>
HeaderTag 类:
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
public class HeaderTag extends TagSupport {
public int doEndTag() throws JspTagException {
try {
pageContext.getOut().print("<p>header</p>");
}
catch (IOException e) {
throw new JspTagException("Error.");
}
return EVAL_PAGE;
}
}