美文网首页
Springboot 加载静态资源 404问题

Springboot 加载静态资源 404问题

作者: yfmei | 来源:发表于2018-11-12 14:15 被阅读0次

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="/myprojectname/css/bootstrap.css" rel="stylesheet">
<script src="/myprojectname/js/jquery.js">
```

静态资源404问题总结

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

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

相关文章

网友评论

      本文标题:Springboot 加载静态资源 404问题

      本文链接:https://www.haomeiwen.com/subject/chfcfqtx.html