使用JSTL访问从Spring Controller到JSP的Map时出错

时间:2011-09-01 18:22:09

标签: spring jsp jstl

我有一个弹簧控制器,我在其中返回一个名为“model”的Map; model包含一些值,但重要的是还包含ArrayList。现在我将这个Map返回给我的JSP,同时在我的JSP中使用JSTL获取ArrayList它会抛出错误。

<c:forEach items="${model.result}" var="data" varStatus="status"> <!-- result is my ArrayList -->
    <c:out value="${data.url}" /> <!-- here url is data inside my result arraylist -->
</c:forEach> 

产生的错误是:

root cause 

java.lang.NoSuchMethodError: javax.servlet.jsp.PageContext.getELContext()Ljavax/el/ELContext;
    at javax.servlet.jsp.jstl.core.LoopTagSupport.unExposeVariables(LoopTagSupport.java:587)
    at javax.servlet.jsp.jstl.core.LoopTagSupport.doFinally(LoopTagSupport.java:323)
    at jsp_servlet._jsp.__searchsuccess._jsp__tag3(__searchsuccess.java:294)
    at jsp_servlet._jsp.__searchsuccess._jspService(__searchsuccess.java:137)
    at weblogic.servlet.jsp.JspBase.service(JspBase.java:34)

它在Tomcat Server上运行正常但是当我在Weblogic(9.2)服务器上部署它时会出错。我的类路径条目是:

<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
    <classpathentry kind="lib" path="WebContent/WEB-INF/lib/commons-fileupload-1.1.1.jar"/>
    <classpathentry kind="lib" path="WebContent/WEB-INF/lib/commons-io-1.2.jar"/>
    <classpathentry kind="lib" path="WebContent/WEB-INF/lib/commons-logging-1.1.1.jar"/>
    <classpathentry kind="lib" path="WebContent/WEB-INF/lib/hibernate-validator-4.0.2.GA.jar"/>
    <classpathentry kind="lib" path="WebContent/WEB-INF/lib/log4j-1.2.14.jar"/>
    <classpathentry kind="lib" path="WebContent/WEB-INF/lib/slf4j-api-1.5.6.jar"/>
    <classpathentry kind="lib" path="WebContent/WEB-INF/lib/slf4j-log4j12-1.5.6.jar"/>
    <classpathentry kind="lib" path="WebContent/WEB-INF/lib/spring-asm-3.0.3.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebContent/WEB-INF/lib/spring-beans-3.0.3.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebContent/WEB-INF/lib/spring-context-3.0.3.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebContent/WEB-INF/lib/spring-core-3.0.3.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebContent/WEB-INF/lib/spring-expression-3.0.3.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebContent/WEB-INF/lib/spring-web-3.0.3.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebContent/WEB-INF/lib/spring-webmvc-3.0.3.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebContent/WEB-INF/lib/validation-api-1.0.0.GA.jar"/>
    <classpathentry kind="lib" path="WebContent/WEB-INF/lib/log4j-1.2.9.jar"/>
    <classpathentry kind="lib" path="WebContent/WEB-INF/lib/servlet-2.3.jar"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre6">
        <attributes>
            <attribute name="owner.project.facets" value="jst.java"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="WebContent/WEB-INF/lib/jstl-1.2.jar"/>
    <classpathentry kind="output" path="WebContent/WEB-INF/classes"/>

更新:我删除了jstl-1.2.jar,现在我收到了编译错误:

searchsuccess.jsp:1:5: No tag library could be found with this URI. Possible causes could be that the URI is incorrect, or that there were errors during pa
rsing of the .tld file.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    ^----^
searchsuccess.jsp:1:5: No tag library could be found with this URI. Possible causes could be that the URI is incorrect, or that there were errors during pa
rsing of the .tld file.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    ^----^

        at weblogic.servlet.jsp.JavelinxJSPStub.compilePage(JavelinxJSPStub.java:298)
        at weblogic.servlet.jsp.JspStub.prepareServlet(JspStub.java:200)
        at weblogic.servlet.jsp.JspStub.prepareServlet(JspStub.java:164)
        at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:235)
        at weblogic.servlet.internal.ServletStubImpl.onAddToMapException(ServletStubImpl.java:394)

2 个答案:

答案 0 :(得分:1)

根据堆栈跟踪,

java.lang.NoSuchMethodError: javax.servlet.jsp.PageContext.getELContext()Ljavax/el/ELContext;
    at javax.servlet.jsp.jstl.core.LoopTagSupport.unExposeVariables(LoopTagSupport.java:587)

JSTL <c:forEach>标记期待方法PageContext#getELContext()。此方法在JSP 2.1中引入。因此,此异常表明您正在使用JSTL 1.2,而您的容器不支持JSP 2.1,或者您使用不支持JSP 2.1的容器的servletcontainer特定JSP库散布了运行时类路径。运行时类路径包括webapp的/WEB-INF/lib文件夹和Java的JRE/libJRE/lib/ext文件夹。

您应永远将servletcontainer特定库放在这些文件夹中,例如jsp-api.jarservlet-api.jarel-api.jarj2ee.jar,{{1等等。通常在servletcontainer本身的javaee.jar文件夹中看到的那些文件。例如。 /lib。它使您的webapp无法移植到目标servlet容器。运行此Web应用程序的目标servletcontainer具有的库。如果您这样做是为了克服JSP / Servlet包上的编译错误,那么您应该以不同的方式解决它。另请参阅How do I import the javax.servlet API in my Eclipse project?


更新:根据您的更新,您使用的是Weblogic 9.2,它是一个Servlet 2.4 / JSP 2.0容器。它根本不支持JSP 2.1 / JSTL 1.2。您需要删除JSTL 1.2库。如果我没有弄错的话,Weblogic已经提供了JSTL 1.1。否则,您需要自己将其包含在类路径(Tomcat/lib文件夹)中。您可以在our JSTL wiki page中找到JSTL 1.1下载链接。

答案 1 :(得分:0)

你的网络应用程序中是否有el-api.jar?

请参阅以下帖子:

http://www.coderanch.com/t/526731/JSP/java/java-lang-NoSuchMethodError-javax-servlet

-Kaj:)