2

我正在尝试将文件中的 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 标头来解决问题。

4

2 回答 2

1

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

我需要动态包含,所以 @include 不是一个选项,并且 jsp:include 在尝试包含由 ResourceHttpRequestHandler 处理的 URL 时会产生 IllegalStateException。所以我只好编写自己的包含,从文件中读取并在 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());
 %>
于 2014-01-17T10:15:00.530 回答
0

尝试在 URL 末尾添加一个“/”,例如 Yours --<c:import url="/path/to/file.js"/> 尝试使用 --<c:import url="/path/to/file.js/"/>

于 2017-05-16T10:56:23.940 回答