tags:

views:

510

answers:

1

Hi,

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

Steps done:

  1. Wrote and compiled successfully a CustomTag.java (extending TagSupport) in WEB-INF/classes directory;
  2. Defined the TLD file, with a very simple example, including <body-content> with an empty value for a bodyless tag;
  3. Used the tag in a JSP page with taglib directive pointing to my /WEB-INF/tlds/site.tld file.

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

CustomTag cannot be resolved to a type

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


Here's my TLD file:

<?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"&gt;

<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:

<%@ 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"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>title</title>
</head>

<body>
 <clib:header />
</body>
</html>


The HeaderTag class:

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

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.

<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.


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

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

kdgregory
Unfortunately, this is not that kind of problem. Application was redeployed, classes "recompiled" and TLD file seems to be valid.
elbaid
With the Edit, that was the problem, no package, and now it's working. Thank you.
elbaid