运行JSP时,JSTL抛出异常

时间:2013-07-03 13:03:57

标签: jstl tomcat7

我目前正在测试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。

1 个答案:

答案 0 :(得分:2)

您试图在JSPX文档中以标准JSP语法声明JSTL标记库,该文档需要格式良好的XML(其中每个<element> 必须具有开始和结束标记)。

这是不对的。阅读JSTL core tag library documentation

  

标准语法:

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
     

XML语法:

    <anyxmlelement xmlns:c="http://java.sun.com/jsp/jstl/core" />

您已在<jsp:root>中拥有正确的XML语法。完全摆脱<@ taglib %>行。

请注意,在JSPX中已无法使用 scriptlet (未计入<jsp:scriptlet>元素)。我想知道你在这里要替换的是什么。


更新:运行时期间未解析的XML命名空间表示webapp的运行时类路径中存在问题。实际的JSTL 1.1 / 1.2 impl在运行时类路径中不存在,或者JSTL 1.0 impl(使用不同的命名空间)存在于运行时类路径中,它在类加载中优先。要验证/确认,请查看导出/部署的WAR文件的/WEB-INF/lib文件夹(您可以通过右键单击 export 手动将项目导出为WAR。)

要安装JSTL 1.2,只需在jstl-1.2.jar中删除/WEB-INF/lib即可。就这样。 在项目的 Build Path 属性中乱搞。

另见: