我目前正在开始测试 JSTL 作为 scriptlet 的替代品,并且从(德语)示例页面中我复制了以下代码:
<?xml version="1.0" encoding="utf-8" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fmt="http://java.sun.com/jsp/jstl/fmt" version="2.0" xmlns:a4j="http://richfaces.org/a4j">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">
<head>
<title>JSTL 1.1</title>
</head>
<body>
<h1>Iteration</h1>
<ul>
<c:forEach var="num" begin="1" end="10">
<li>Nummer <c:out value="${num}"/></li>
</c:forEach>
</ul>
<h1>Formatierung</h1>
<p>
Währung: <fmt:formatNumber value="10000" type="currency" currencyCode="EUR" />
</p>
</body>
</html>
</jsp:root>
我的应用程序容器是 Tomcat 7.0,JSTL 库链接到项目(如果不是,则会出现不同的错误消息)。在我的本地安装中从浏览器调用列出的 JSP 会引发以下错误消息:
Jul 03, 2013 2:56:58 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/webapp01] threw exception [org.apache.jasper.JasperException: /test.jsp (line: 3, column: 3) Der Content von Elementen muss aus ordnungsgemäß formatierten Zeichendaten oder Markups bestehen.] with root cause
org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 3; Der Content von Elementen muss aus ordnungsgemäß formatierten Zeichendaten oder Markups bestehen.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
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:441)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1388)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.startOfMarkup(XMLDocumentFragmentScannerImpl.java:2567)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2665)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:489)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:302)
at org.apache.jasper.compiler.JspDocumentParser.parse(JspDocumentParser.java:172)
at org.apache.jasper.compiler.ParserController.doParse(ParserController.java:226)
at org.apache.jasper.compiler.ParserController.parseDirectives(ParserController.java:119)
at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:193)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
它似乎确实在抱怨<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
,但是,我需要实际加载 JSTL 2.0 的命名空间定义,不是吗?
编辑 1:删除 taglib 的手动(和错误格式)导入会导致我试图通过手动导入修复的错误消息:
This page contains the following errors:
error on line 1 at column 110: Namespace prefix c on forEach is not defined
error on line 2 at column 56: Namespace prefix fmt on formatNumber is not defined
我在某处发现这是因为找不到 JSTL。但是,我已将其添加jstl-1.2.jar
到项目中。另外,我读到这个问题可能是由于没有正确的 Tomcat 版本引起的。但是,我很确定我正在运行 Tomcat 7。