Javascript 加载资源失败:服务器响应状态为404?

Javascript 加载资源失败:服务器响应状态为404?,javascript,java,spring,spring-mvc,Javascript,Java,Spring,Spring Mvc,我正在运行一个SpringMVC应用程序,下面是我的项目结构 在我的homepage.jsp中,我尝试在标题部分加载js <head> <script type="text/javascript" src="/resources/myScript.js"></script> </head> 在我的spring文件中,我添加了 <resources mapping="/resources/**" location="/resources/"

我正在运行一个SpringMVC应用程序,下面是我的项目结构

在我的
homepage.jsp
中,我尝试在标题部分加载js

<head>
<script type="text/javascript" src="/resources/myScript.js"></script>
</head>
在我的spring文件中,我添加了

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


有人能指出我犯的错误吗?

在WAR文件中找到JS文件的实际路径。检查是否可以通过调用JS文件的URL直接访问该文件。例如-。然后在JSP中包含脚本文件和EL${pageContext.request.contextPath}。像这样

<script src="${pageContext.request.contextPath}/resources/myScript.js"></script>


类似问题中的更多细节-

最好使用核心JSTL在JSP中提供资源路径,并在XML中定义mvc资源,这样任何mvc拦截器都不会适用于资源的请求

<!-- UI resources exclusions from servlet mapping -->
<mvc:resources location="/resources/" mapping="/resources/**"/>

并使用核心jstl加载资源文件

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<script src="<c:url value='/resources/myScript.js'/>"></script>
</head>


能否显示您的servletcontext.xml!!在浏览器中访问
homepage.jsp
的URL是什么?如果类似
http://localhost:8080/myapp/homepage.jsp
,则
myapp
是应用程序的“上下文路径”,所有绝对路径都必须列出该路径,例如
src=“/resources/myScript.js”
在浏览器看到时应为
src=“/myapp/resources/myScript.js”
。不要在.jsp文件中硬编码该值,因为它可能会更改。如果你搜索,有很多关于这方面的好文章。谢谢@Andreas的帮助。它帮助我解决了这个问题
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<script src="<c:url value='/resources/myScript.js'/>"></script>
</head>