JSP標簽擴展如何解決這個問題?

[英]How to solve this problem with JSP tag extension?


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)包中的任何類是非法的。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/05/17/3716430fc3e9c2c8b74f18733c98f91d.html



 
  © 2014-2022 ITdaan.com 联系我们: