2017-04-06 60 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一切之後。

這很奇怪,像這樣的小錯誤如何影響我的整個程序。

希望它對某些面臨同樣問題的人有用。

相關問題