Springboot 加载静态资源 404问题

springboot 工程

目录结构

├─static
      ├─css
      └─js
└─templates
  • templates 模板文件目录 thymeleaf/freemarker

  • springboot查找静态资源的相关配置项:

# 应用上下文配置
server.servlet.context-path=/myprojectname

# 默认配置会自动在 /public /static /resources 目录寻找静态资源, 故不需要 /static 等前缀
spring.mvc.static-path-pattern=/**

# SpringMvc(ModelAndView) 视图前缀 prefix/xxx/xxx.html, 可不设,如果static目录下有以工程名命名的文件夹,则可以设置(如: /static/project/css)
spring.mvc.view.prefix=${server.servlet.context-path}

html 引用静态文件

```
<link href="//www.greatytc.com/myprojectname/css/bootstrap.css" rel="stylesheet">
<script src="/myprojectname/js/jquery.js">
```

静态资源404问题总结

  • 若设置 server.servlet.context-path=/myprojectname 则每个静态资源都需要加上该值, 否则会出现404的问题
<link href="//www.greatytc.com/myprojectname/css/bootstrap.css" rel="stylesheet">
  • ./ 和 / 的区别

    如果每个 Controller 都没有 RequestMapping 则html中可以使用 ./ , 但这样不利用业务模块划分.

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容