没有映射静态资源 Spring Boot

huangapple go评论82阅读模式
英文:

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

这是静态资源的位置:

没有映射静态资源 Spring Boot

我做错了什么?

英文:

In my Spring-Boot application, js and css files do not work, it says 404 not found.

My html-page includes the following:

  &lt;head&gt;  
     &lt;link href=&quot;/webjars/bootstrap/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;&gt;
     &lt;link href=&quot;/static/css/style.css&quot; rel=&quot;stylesheet&quot;&gt;
     &lt;script src=&quot;/webjars/jquery/jquery.min.js&quot;&gt;&lt;/script&gt;
     &lt;script src=&quot;/webjars/sockjs-client/sockjs.min.js&quot;&gt;&lt;/script&gt;
     &lt;script src=&quot;/webjars/stomp-websocket/stomp.min.js&quot;&gt;&lt;/script&gt;
     &lt;script src=&quot;/static/js/operations.js&quot;&gt;&lt;/script&gt;
 &lt;/head&gt;

I configured resources so:

 @Configuration
 @EnableWebMvc
 public class MvcConfig implements WebMvcConfigurer {
     @Override
     public void addResourceHandlers(ResourceHandlerRegistry registry) {
         registry
               .addResourceHandler(&quot;/resources/**&quot;)
               .addResourceLocations(&quot;/resources/&quot;);
        }
  }

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:

没有映射静态资源 Spring Boot

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:

&lt;link href=&quot;/css/style.css&quot; rel=&quot;stylesheet&quot;&gt;

instead of

&lt;link href=&quot;/static/css/style.css&quot; rel=&quot;stylesheet&quot;&gt;

答案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=&quot;${pageContext.request.contextPath}/resources/static/...&quot;

so try using below path

href=&quot;${pageContext.request.contextPath}/resources/static/css/style.css&quot;

There may be better way of resolving it but till then this is a quick fix.

huangapple
  • 本文由 发表于 2020年4月9日 05:50:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/61110615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定