Come risolvere questo problema con JSP estensione etichetta?
-
22-08-2019 - |
Domanda
che iniziano con le JSP e lo sviluppo servlet, ho alcuni problemi con un tag personalizzato senza corpo da inserire in una pagina JSP.
I passaggi effettuati:
- ha scritto e compilato un (
TagSupport
si estende) CustomTag.java nella directoryWEB-INF/classes
; - Definito il file TLD, con un esempio molto semplice, tra cui
<body-content>
con un valoreempty
per un tag senza corpo; - Utilizzato il tag in una pagina JSP con la direttiva taglib che punta al mio file
/WEB-INF/tlds/site.tld
.
Con tutto questo in mente, hai un indizio perché Tomcat è l'invio di un errore come questo:
CustomTag non può essere risolto a un tipo
Grazie in anticipo per le vostre risposte, e si prega di chiedere se avete bisogno di ulteriori dettagli.
Ecco il mio 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>
Il 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>
La classe 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;
}
}
Soluzione
Hai ricostruita e ridistribuito, giusto? In quel caso la mia ipotesi migliore è che hai lasciato fuori il
<tag>
<name>cookieIterator</name>
<tag-class>util.infoTemplates.CookieIterator</tag-class>
<body-content>JSP</body-content>
</tag>
Se questo non è la causa, si prega di inviare il file TLD e un esempio JSP.
Modifica: Tutte le classi tag deve avere un pacchetto. Per il 2.0 spec JSP (JSP sezione 11.2):
A partire dal JSP 2.0, è illegale per riferirsi a tutte le classi dal senza nome (anche noto come default) del pacchetto.