2017-04-06 88 views
4

我之前看到过许多对此问题的答案,但没有一个解决方案尝试过。Springboot - 将资源解释为样式表,但使用MIME类型传输文本/ htm

我的login.css未在我的Spring Boot应用程序中应用到login.html。我也在使用Thymeleaf。

安全性已打开 - 但我不认为这是问题,因为在浏览器中。我没有看到一个失败加载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.css302状态码

感谢

回答

1

这个我是如何解决这个问题:

我把我的css文件中:

/static/css/login.css 

然后在HTML它被引用为:

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

谢谢。出来了!

0

我有相同的问题,而与春天引导+ Spring MVC的写一些代码。使用CDN设置的CSS文件工作正常,而从我的static/css文件夹中设置的CSS文件返回了HTML内容。

例子:

<!-- 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"; 
} 

开始被正确地加载更改属性namepath一切之后。

这很奇怪,像这样的小错误如何影响我的整个程序。

希望它对某些面临同样问题的人有用。

相关问题