SpringBoot中的html页面访问不到js,css等静态资源问题解决方案

首先贴上项目目录结构

当我在index.html中访问css中的index.css时,发现报404,路径是http://localhost:8080/static/css/index.css

 <link rel="stylesheet" href="/static/css/index.css" />
  
  

网上的解释是在SpringBoot中,默认配置的/**映射到/static,所以不需要加/static,但我去掉后,发现仍然报404,但html在加载css时访问的路径是http://localhost:8080/css/index.css

此时,我觉得可能是我springboot中配置了context-path导致

所以又在html中加了/SIMS

 <link rel="stylesheet" href="/SIMS/css/normalize.css" />
  
  

果然,加了之后成功访问到了。

同时,如果将html访问css改成通过../访问,通过访问上一级也是能成功的

 <link rel="stylesheet" href="../css/index.css" />
  
  

至此,访问css问题解决,但新问题又出现了。当我想通过a标签跳转到templates文件夹下的其他html时,发现又报了404。

查找网上资料发现,templates下的页面只能通过Controller跳转实现,而static下的页面是能直接被外界访问的,故放弃了templates文件夹,直接将html放置于static下,就能正常访问了。

之后得到项目新结构

希望可以帮助到你们。

转载自:https://blog.csdn.net/aGreySky/article/details/89236815

发布了46 篇原创文章 · 获赞 41 · 访问量 8411

首先贴上项目目录结构

猜你喜欢

转载自blog.csdn.net/weixin_42635052/article/details/104113210