Sping Boot 页面css无法加载 because its MIME type ('application/json') is not a supported stylesheet ...

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chenbetter1996/article/details/85092531

错误描述

Refused to apply style from 'http://localhost:8080/static/css/login.css' 
because its MIME type ('application/json') is not a supported stylesheet MIME type, 
and strict MIME checking is enabled.

显然 http://localhost:8080/static/css/login.css 被拦截了,所以无法加载应用。


在这里插入图片描述

因为Spring Boot默认拦截static下的静态资源的这种访问方式。单只是开发前端页面的时候
href="…/static/css/xx.css" (对于templates下的HTML文件)是没问题的。

但是如果运行时是 host://port/css/xx.css 是可以访问到资源的。
就是static文件夹是应该缺省的。但是这Spring Boot运行时有效。普通前端页面这样相对路径引用肯定不行。


解决方法

<!-- thymeleaf引用法 -->
<link rel="stylesheet" type="text/css" th:href="@{css/login.css}">
<link rel="stylesheet" type="text/css" href="/css/login.css">
<link rel="stylesheet" type="text/css" href="css/login.css">

这三种对运行时都有效

<link rel="stylesheet" type="text/css" href="../static/css/login.css">

页面设计时还是得照常的相对路径

总结:Spring Boot项目默认运行时static文件夹是缺省的。所以编辑静态文件的引用的相对路径还是已运行时为准。页面设计时就照常加static的相对路径。

猜你喜欢

转载自blog.csdn.net/chenbetter1996/article/details/85092531