JSP 而非 运行 中的 Taglib
Taglib in JSP not running
我正在使用 netBeans 和 glassFish Server 4.1.1
<%@ taglib uri="index.html" prefix="mytag" %>
<html>
<body>
<mytag:currentDate/>
</body>
</html>
我得到了这样的错误:
org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 7; The element type "meta" must be terminated by the matching end-tag "". at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327) at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1438) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1750) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2970) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:118) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141) at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339) at org.apache.jasper.xmlparser.ParserUtils.parseXMLDocument(ParserUtils.java:298) at org.apache.jasper.xmlparser.ParserUtils.parseXMLDocument(ParserUtils.java:351) at org.apache.jasper.compiler.TagLibraryInfoImpl.parseTLD(TagLibraryInfoImpl.java:340) at org.apache.jasper.compiler.TagLibraryInfoImpl.(TagLibraryInfoImpl.java:254) at org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:502) at org.apache.jasper.compiler.Parser.parseDirective(Parser.java:582) at org.apache.jasper.compiler.Parser.parseElements(Parser.java:1657) at org.apache.jasper.compiler.Parser.parse(Parser.java:185) at org.apache.jasper.compiler.ParserController.doParse(ParserController.java:244) at org.apache.jasper.compiler.ParserController.parse(ParserController.java:145) at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:212) at org.apache.jasper.compiler.Compiler.compile(Compiler.java:451) at org.apache.jasper.JspC.processFile(JspC.java:1171) at org.apache.jasper.JspC.execute(JspC.java:1338) at org.netbeans.modules.web.project.ant.JspC.main(JspC.java:101) at org.netbeans.modules.web.project.ant.JspCSingle.main(JspCSingle.java:119) org.apache.jasper.JasperException: file:C:/Users/Queue/OneDrive/JspExE/build/web/WEB-INF/tlb.jsp(1,44) PWC6178: XML parsing error on file /WEB-INF/index.html: (line 12, col 7) org.apache.jasper.JasperException: C:/Users/Queue/OneDrive/JspExE/build/web/WEB-INF/tlb.jsp(1,44) org.apache.jasper.JasperException: file:C:/Users/Queue/OneDrive/JspExE/build/web/WEB-INF/tlb.jsp(1,44) PWC6178: XML parsing error on file C:\Users\Queue\OneDrive\JspExE\nbproject\build-impl.xml:936: Java returned: 1 BUILD FAILED (total time: 1 second)
文件 index.html
是一个 html 文件。您不能在 taglib
指令的 URI 属性中使用 HTML 而不是 TLD。
The uri attribute refers to a URI that uniquely identifies the tag
library descriptor (TLD), a document that describes the tag library
(see Tag Library Descriptors).
Tag library descriptor file names must have the extension .tld
. TLD
files are stored in the WEB-INF
directory or subdirectory of the WAR
file, or in the META-INF
directory or subdirectory of a tag library
packaged in a JAR. You can reference a TLD directly or indirectly.
The following taglib
directive directly references a TLD file name:
<%@ taglib prefix="tlt" uri="/WEB-INF/iterator.tld"%>
This taglib
directive uses a short logical name to indirectly
reference the TLD:
<%@ taglib prefix="tlt" uri="/tlt"%>
Nested inside a jsp-config
element is a taglib
element, which provides
information on a tag library used by the pages of the application.
Inside the taglib
element are the taglib-uri
element and the
taglib-location
element. The taglib-uri
element identifies the logical
name of the tag library. The taglib-location
element gives the
absolute location or the absolute URI of the tag library.
The absolute URIs for the JSTL library are as follows:
Core: http://java.sun.com/jsp/jstl/core
XML: http://java.sun.com/jsp/jstl/xml
Internationalization: http://java.sun.com/jsp/jstl/fmt
SQL: http://java.sun.com/jsp/jstl/sql
Functions: http://java.sun.com/jsp/jstl/functions
When you reference a tag library with an absolute URI that exactly
matches the URI declared in the taglib
element of the TLD (see Tag
Library Descriptors), you do not have to add the taglib
element to
web.xml
; the JSP container automatically locates the TLD inside the
JSTL library implementation. Including the Tag Library Implementation
In addition to declaring the tag library, you also must make the tag
library implementation available to the web application. There are
several ways to do this. Tag library implementations can be included
in a WAR in an unpacked format: Tag files are packaged in the
/WEB-INF/tag/
directory, and tag handler classes are packaged in the
/WEB-INF/classes/
directory of the WAR. Tag libraries already packaged
into a JAR file are included in the /WEB-INF/lib/
directory of the
WAR. Finally, an application server can load a tag library into all
the web applications running on the server. For example, in the
Application Server, the JSTL TLDs and libraries are distributed in the
archive appserv-jstl.jar
in as-install/lib/
. This library is
automatically loaded into the classpath of all web applications
running on the Application Server, so you don’t need to add it to your
web application.
我正在使用 netBeans 和 glassFish Server 4.1.1
<%@ taglib uri="index.html" prefix="mytag" %>
<html>
<body>
<mytag:currentDate/>
</body>
</html>
我得到了这样的错误:
org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 7; The element type "meta" must be terminated by the matching end-tag "". at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327) at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1438) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1750) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2970) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:118) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141) at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339) at org.apache.jasper.xmlparser.ParserUtils.parseXMLDocument(ParserUtils.java:298) at org.apache.jasper.xmlparser.ParserUtils.parseXMLDocument(ParserUtils.java:351) at org.apache.jasper.compiler.TagLibraryInfoImpl.parseTLD(TagLibraryInfoImpl.java:340) at org.apache.jasper.compiler.TagLibraryInfoImpl.(TagLibraryInfoImpl.java:254) at org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:502) at org.apache.jasper.compiler.Parser.parseDirective(Parser.java:582) at org.apache.jasper.compiler.Parser.parseElements(Parser.java:1657) at org.apache.jasper.compiler.Parser.parse(Parser.java:185) at org.apache.jasper.compiler.ParserController.doParse(ParserController.java:244) at org.apache.jasper.compiler.ParserController.parse(ParserController.java:145) at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:212) at org.apache.jasper.compiler.Compiler.compile(Compiler.java:451) at org.apache.jasper.JspC.processFile(JspC.java:1171) at org.apache.jasper.JspC.execute(JspC.java:1338) at org.netbeans.modules.web.project.ant.JspC.main(JspC.java:101) at org.netbeans.modules.web.project.ant.JspCSingle.main(JspCSingle.java:119) org.apache.jasper.JasperException: file:C:/Users/Queue/OneDrive/JspExE/build/web/WEB-INF/tlb.jsp(1,44) PWC6178: XML parsing error on file /WEB-INF/index.html: (line 12, col 7) org.apache.jasper.JasperException: C:/Users/Queue/OneDrive/JspExE/build/web/WEB-INF/tlb.jsp(1,44) org.apache.jasper.JasperException: file:C:/Users/Queue/OneDrive/JspExE/build/web/WEB-INF/tlb.jsp(1,44) PWC6178: XML parsing error on file C:\Users\Queue\OneDrive\JspExE\nbproject\build-impl.xml:936: Java returned: 1 BUILD FAILED (total time: 1 second)
文件 index.html
是一个 html 文件。您不能在 taglib
指令的 URI 属性中使用 HTML 而不是 TLD。
The uri attribute refers to a URI that uniquely identifies the tag library descriptor (TLD), a document that describes the tag library (see Tag Library Descriptors).
Tag library descriptor file names must have the extension
.tld
. TLD files are stored in theWEB-INF
directory or subdirectory of the WAR file, or in theMETA-INF
directory or subdirectory of a tag library packaged in a JAR. You can reference a TLD directly or indirectly.The following
taglib
directive directly references a TLD file name:<%@ taglib prefix="tlt" uri="/WEB-INF/iterator.tld"%>
This
taglib
directive uses a short logical name to indirectly reference the TLD:<%@ taglib prefix="tlt" uri="/tlt"%>
Nested inside a
jsp-config
element is ataglib
element, which provides information on a tag library used by the pages of the application. Inside thetaglib
element are thetaglib-uri
element and thetaglib-location
element. Thetaglib-uri
element identifies the logical name of the tag library. Thetaglib-location
element gives the absolute location or the absolute URI of the tag library.The absolute URIs for the JSTL library are as follows:
Core: http://java.sun.com/jsp/jstl/core XML: http://java.sun.com/jsp/jstl/xml Internationalization: http://java.sun.com/jsp/jstl/fmt SQL: http://java.sun.com/jsp/jstl/sql Functions: http://java.sun.com/jsp/jstl/functions
When you reference a tag library with an absolute URI that exactly matches the URI declared in the
taglib
element of the TLD (see Tag Library Descriptors), you do not have to add thetaglib
element toweb.xml
; the JSP container automatically locates the TLD inside the JSTL library implementation. Including the Tag Library ImplementationIn addition to declaring the tag library, you also must make the tag library implementation available to the web application. There are several ways to do this. Tag library implementations can be included in a WAR in an unpacked format: Tag files are packaged in the
/WEB-INF/tag/
directory, and tag handler classes are packaged in the/WEB-INF/classes/
directory of the WAR. Tag libraries already packaged into a JAR file are included in the/WEB-INF/lib/
directory of the WAR. Finally, an application server can load a tag library into all the web applications running on the server. For example, in the Application Server, the JSTL TLDs and libraries are distributed in the archiveappserv-jstl.jar
inas-install/lib/
. This library is automatically loaded into the classpath of all web applications running on the Application Server, so you don’t need to add it to your web application.