英文:
No mapping for static-resources Spring Boot
问题
在我的Spring Boot应用程序中,js和css文件不起作用,显示404未找到错误。
我的html页面包含以下内容:
<head>
<link href="/webjars/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="/static/css/style.css" rel="stylesheet">
<script src="/webjars/jquery/jquery.min.js"></script>
<script src="/webjars/sockjs-client/sockjs.min.js"></script>
<script src="/webjars/stomp-websocket/stomp.min.js"></script>
<script src="/static/js/operations.js"></script>
</head>
我配置了资源如下:
@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
}
但是在日志中,我收到了以下消息:
o.s.web.servlet.PageNotFound: No mapping for GET /favicon.ico
o.s.web.servlet.PageNotFound: No mapping for GET /favicon.ico
o.s.web.servlet.PageNotFound: No mapping for GET /webjars/jquery/jquery.min.js
o.s.web.servlet.PageNotFound: No mapping for GET /static/css/style.css
o.s.web.servlet.PageNotFound: No mapping for GET /static/js/operations.js
o.s.web.servlet.PageNotFound: No mapping for GET /favicon.ico
这是静态资源的位置:
我做错了什么?
英文:
In my Spring-Boot application, js and css files do not work, it says 404 not found.
My html-page includes the following:
<head>
<link href="/webjars/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="/static/css/style.css" rel="stylesheet">
<script src="/webjars/jquery/jquery.min.js"></script>
<script src="/webjars/sockjs-client/sockjs.min.js"></script>
<script src="/webjars/stomp-websocket/stomp.min.js"></script>
<script src="/static/js/operations.js"></script>
</head>
I configured resources so:
@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
}
But in logs I receive:
o.s.web.servlet.PageNotFound : No mapping for GET /favicon.ico
o.s.web.servlet.PageNotFound : No mapping for GET /favicon.ico
o.s.web.servlet.PageNotFound : No mapping for GET /webjars/jquery/jquery.min.js
o.s.web.servlet.PageNotFound : No mapping for GET /static/css/style.css
o.s.web.servlet.PageNotFound : No mapping for GET /static/js/operations.js
o.s.web.servlet.PageNotFound : No mapping for GET /favicon.ico
This is the location of static-sources:
What am I doing wrong?
答案1
得分: 3
默认情况下,此处理程序从类路径上的/static
、/public
、/resources
和/META-INF/resources
目录中提供静态内容。由于通常情况下src/main/resources
位于默认的类路径上,我们可以将这些目录中的任何内容放在那里。
这意味着你的链接应该是:
<link href="/css/style.css" rel="stylesheet">
而不是
<link href="/static/css/style.css" rel="stylesheet">
英文:
By default, this handler serves static content from any of /static, /public, /resources, and /META-INF/resources
directories that are on the classpath. Since src/main/resources
is typically on the classpath by default, we can place any of these directories there.
This means that your links should look like:
<link href="/css/style.css" rel="stylesheet">
instead of
<link href="/static/css/style.css" rel="stylesheet">
答案2
得分: 1
我能够通过使用以下路径解决项目中的问题:
href="${pageContext.request.contextPath}/resources/static/..."
所以尝试使用以下路径:
href="${pageContext.request.contextPath}/resources/static/css/style.css"
可能有更好的解决方法,但在那之前这是一个快速修复。
英文:
I was able to resolve issue in my project by using below path
href="${pageContext.request.contextPath}/resources/static/..."
so try using below path
href="${pageContext.request.contextPath}/resources/static/css/style.css"
There may be better way of resolving it but till then this is a quick fix.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论