javascript Spring 3.1-找不到javascript文件-404错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11952212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:52:14  来源:igfitidea点击:

Spring 3.1- javascript file not found- 404 error

javascriptspringjspspring-mvc

提问by shazinltc

I have a js file in the folder WebContent/resources/js/test.js. I am trying to include the same file in a jsp. But the jsp file is unable to find the js file (404 error in browser console). I have gone throw couple of questions in SO:

我在文件夹 WebContent/resources/js/test.js 中有一个 js 文件。我正在尝试在 jsp 中包含相同的文件。但是jsp文件找不到js文件(浏览器控制台404错误)。我已经在 SO 中提出了几个问题:

Can SpringMVC be configured to process all requests, but exclude static content directories?

SpringMVC 可以配置为处理所有请求,但排除静态内容目录吗?

Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP

调用转发到 JSP 的 Servlet 时,浏览器无法访问/查找 CSS、图像和链接等相关资源

STS Spring MVC: How to include a JS file in a JSP

STS Spring MVC:如何在 JSP 中包含 JS 文件

But still not helping. Here goes my code:

但仍然没有帮助。这是我的代码:

In the application context, i am using the mvc:resource tag.

在应用程序上下文中,我使用的是 mvc:resource 标记。

<mvc:resources mapping="/resources/**" location="/resources/" />

In my jsp

在我的jsp中

<script src="${contextPath}/resources/js/test.js" type="text/javascript"></script>

tried giving src value with

尝试给 src 值

 <c:url> 

too.

也。

my web.xml has

我的 web.xml 有

<servlet-mapping> 
<servlet-name>TestProject</servlet-name> 
<url-pattern>/</url-pattern>
</servlet-mapping>

Firebug says

萤火虫 说

The requested resource (/resources/js/test.js) is not available.

Any help would be appreciated.

任何帮助,将不胜感激。

UPDATE

更新

The GET request URL in firebug is this

firebug 中的 GET 请求 URL 是这个

http://localhost:8080/TestProject/resources/js/test.js

Is it right??

这样对吗??

回答by dardo

You need to do

你需要做

<script src="<c:url value="/resources/js/test.js" />" type="text/javascript"></script>

Spring is trying to find the resource from the container context root, rather than the app context root.

Spring 试图从容器上下文根而不是应用程序上下文根中查找资源。