我尝试导入静态文件时的JSP异常

时间:2013-06-20 16:12:14

标签: java jsp exception jstl

我正在尝试将javascript从文件导入到我的jsp中以用作内联脚本:

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

<script>
    <c:import url="/path/to/file.js" />
</script>

以上代码99%的时间都可以工作但我在tomcat日志中看到了一些这样的错误:

Jun 20, 2013 1:25:33 AM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
javax.servlet.jsp.JspTagException: 304 /path/to/file.js
    at org.apache.taglibs.standard.tag.common.core.ImportSupport.acquireString(ImportSupport.java:329)
    at org.apache.taglibs.standard.tag.common.core.ImportSupport.doEndTag(ImportSupport.java:171)
    at org.apache.jsp.WEB_002dINF.jsp.common.head.scripts_jsp._jspx_meth_c_005fimport_005f0(scripts_jsp.java:182)
    at org.apache.jsp.WEB_002dINF.jsp.common.head.scripts_jsp._jspService(scripts_jsp.java:85)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    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.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:605)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:544)
    at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:954)
    at org.apache.jsp.WEB_002dINF.jsp.game_jsp._jspService(game_jsp.java:181)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    .......

我在ImportSupport.java中找到了抛出上述异常的代码:

// disallow inappropriate response codes per JSTL spec
if (irw.getStatus() < 200 || irw.getStatus() > 299) {
    throw new JspTagException(irw.getStatus() + " " + stripSession(targetUrl));
}

因此看起来servlet响应代码是304。

问题是为什么?这是一个错误还是我错过了什么?

更新

如果传入请求在

上具有If-Modified-Since标头,则似乎只会出现此问题

更新2:

我通过从除静态文件之外的每个请求中删除If-Modified-Since标头来解决问题。

2 个答案:

答案 0 :(得分:1)

我正在使用Spring MVC的ResourceHttpRequestHandler(使用mvc:resources配置)从/ resources / *提供静态资源,并在使用If-Modified-Since标头的传入请求中返回304。因此,当我使用c:import包含这样的静态资源并且传入请求具有If-Modified-Since标头时,我得到javax.servlet.jsp.JspTagException:304错误。

我需要动态包括所以@include不是一个选项,并且当尝试包含由ResourceHttpRequestHandler处理的URL时,jsp:include会产生IllegalStateException。所以我只留下编写自己的include,从文件中读取并将其写在JSP中:

<%@ page import="org.springframework.web.context.support.ServletContextResourceLoader, org.springframework.core.io.Resource" %>
<%@ page import="org.apache.commons.io.IOUtils" %>

<%
    String path = "/resources/fileName.html";
    ServletContextResourceLoader loader = new ServletContextResourceLoader(getServletConfig().getServletContext());
    Resource resource = loader.getResource(path);
    IOUtils.copy(contentResource.getInputStream(), pageContext.getOut());
 %>

答案 1 :(得分:0)

尝试在结尾处添加一个“/” 例如    你的 - <c:import url="/path/to/file.js"/> 试试 - <c:import url="/path/to/file.js/"/>