JSP标签扩展如何解决这个问题?

时间:2022-11-26 10:34:18

Begining with JSP and servlet development, I have some problems with a bodyless custom tag to be inserted in a JSP page.

开始使用JSP和servlet开发,我在将一个无体自定义标记插入JSP页面时遇到了一些问题。

Steps done:

  1. Wrote and compiled successfully a CustomTag.java (extending TagSupport) in WEB-INF/classes directory;
  2. 在WEB-INF / classes目录中成功编写并编译了CustomTag.java(扩展TagSupport);

  3. Defined the TLD file, with a very simple example, including <body-content> with an empty value for a bodyless tag;
  4. 定义了TLD文件,其中包含一个非常简单的示例,包括 ,其中包含无体标记的空值;

  5. Used the tag in a JSP page with taglib directive pointing to my /WEB-INF/tlds/site.tld file.
  6. 在JSP页面中使用标记,taglib指令指向我的/WEB-INF/tlds/site.tld文件。

With all this in mind, do you have a clue why Tomcat is sending an error like this:

考虑到所有这些,你有一个线索,为什么Tomcat发送这样的错误:

CustomTag cannot be resolved to a type

CustomTag无法解析为某种类型

Thanks in advance for your answers, and please ask if you need more details.

在此先感谢您的回答,请询问您是否需要更多详细信息。


Here's my TLD file:

这是我的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>

The JSP file:

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>

The HeaderTag class:

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;
    }
}

1 个解决方案

#1


You've rebuilt and redeployed, correct? In that case my best guess is that you left out the <tag-class> directive in the TLD file.

你重建并重新部署了,对吗?在这种情况下,我最好的猜测是你遗漏了TLD文件中的 指令。

<tag>
    <name>cookieIterator</name>
    <tag-class>util.infoTemplates.CookieIterator</tag-class>
    <body-content>JSP</body-content>
</tag>

If that isn't the cause, please post your TLD file and an example JSP.

如果这不是原因,请发布您的TLD文件和示例JSP。


Edit: All tag classes must have a package. Per the JSP 2.0 spec (section JSP 11.2):

编辑:所有标记类都必须包含一个包。根据JSP 2.0规范(JSP 11.2节):

As of JSP 2.0, it is illegal to refer to any classes from the unnamed (a.k.a. default) package.

从JSP 2.0开始,引用未命名(a.k.a.default)包中的任何类是非法的。

#1


You've rebuilt and redeployed, correct? In that case my best guess is that you left out the <tag-class> directive in the TLD file.

你重建并重新部署了,对吗?在这种情况下,我最好的猜测是你遗漏了TLD文件中的 指令。

<tag>
    <name>cookieIterator</name>
    <tag-class>util.infoTemplates.CookieIterator</tag-class>
    <body-content>JSP</body-content>
</tag>

If that isn't the cause, please post your TLD file and an example JSP.

如果这不是原因,请发布您的TLD文件和示例JSP。


Edit: All tag classes must have a package. Per the JSP 2.0 spec (section JSP 11.2):

编辑:所有标记类都必须包含一个包。根据JSP 2.0规范(JSP 11.2节):

As of JSP 2.0, it is illegal to refer to any classes from the unnamed (a.k.a. default) package.

从JSP 2.0开始,引用未命名(a.k.a.default)包中的任何类是非法的。