Springboot - 资源解释为样式表,但使用 MIME 类型 text/htm 进行传输

2023-12-31

我之前看到过这个问题的很多答案,但我尝试过的解决方案都不起作用。

My login.css没有被应用到login.html在我的 Spring Boot 应用程序中。我也在使用百里香叶。

安全性已打开 - 但我认为这不是浏览器中的问题。我没有看到加载失败login.css- 仅消息:

资源解释为样式表,但使用 MIME 类型 text/html 进行传输:

在浏览器中进一步检查表明它正在请求text/css,但得到一个text/html回复。

html:

<!doctype html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">

<head>

<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatiable" content="IE-Edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="Login"/>


<title>LOGIN</title>

<!-- Latest compiled and minified CSS -->

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

的路径为login.html

\src\main\resources\templates\login.html

路径为login.css

\src\main\resources\static\login.css

以防万一这是一个权限问题,我提出:

.antMatchers("/css/**").permitAll()

我注意到通过 CDN 传递的所有 CSS 都没有问题。浏览器也返回login.css with a 302状态码.

Thanks


我在编写一些代码时遇到了完全相同的问题Spring Boot + Spring MVC。使用 CDN 设置的 CSS 文件工作正常,而从我的 CSS 文件设置static/css文件夹返回了 HTML 内容。

Example:

<!-- This first worked fine, loading all styles -->
<link th:href="@{/webjars/bootstrap/3.3.7/css/bootstrap.min.css}"
      href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.7/css/bootstrap.min.css"
      rel="stylesheet" media="screen" />

<!-- This second one returned the content of an HTML - Content Type text/html -->
<link rel="stylesheet" th:href="@{/css/login/style.css}" href="/css/login/style.css" />

过了一会儿我可以看到使用Chrome 开发工具内容返回给我的本地style.css与我的 HTML 页面之一相同。

检查指向包含该内容的 HTML 文件的路由,我发现我使用了错误的属性@RequestMapping配置。我有@RequestMapping(name="..."), 代替@RequestMapping(path="...").

有问题的控制器

@RequestMapping(name = "/product/add", method = RequestMethod.GET)
public String add(Model model) {
    model.addAttribute("product", new Product());
    return "product/edit";
}

控制器已更改

@RequestMapping(path = "/product/add", method = RequestMethod.GET)
public String add(Model model) {
    model.addAttribute("product", new Product());

    return "product/edit";
}

变更房产后name with path一切开始正确加载。

奇怪的是,像这样的小错误竟然影响了我的整个程序。

希望它对面临同样问题的人有所帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Springboot - 资源解释为样式表,但使用 MIME 类型 text/htm 进行传输 的相关文章

随机推荐