2017-09-18 29 views
1

我見過的問題與建議將絕對路徑轉換爲相對路徑的答案,但這對我來說是不可能的,因爲我的HTML文件存儲在各種目錄中並使用一行代表CSS的模板。所以fragments.html開始是這樣的:Thymeleaf:在css文件中的絕對引用

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" 
    xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> 
<head th:fragment="common_header(title)"> 
    <link rel="stylesheet" th:href="@{/css/style.css}" /> 
: 

實際的HTML看起來像

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" 
    xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> 
<head th:replace="fragments :: common_header('List Items')"> 
    <title>Main</title> 
</head> 
<body> 
    <script th:src="@{/js/rss/list.js}"></script> 
: 

現在/css/style.css

.item-subject::before { 
    content: url("/images/new.png"); 
    margin-right: 5px; 
} 

隨着上下文路徑和所有很明顯,這不起作用。但我能做什麼呢?我的意思是th:href使得link指向一個有效的資源,所以現在CSS的內容也應該以某種方式來保存。

很明顯,content -line必須包含魔法,但我該如何做所有這些工作?

回答

1

can parse css files with thymeleaf,就像你可以解析html文件一樣。 Thymeleaf 3專門有一個CSS模式。你必須建立控制器就像你的HTML文件(here is a sample project),但在那之後,你可以在你的CSS直接使用URL表達式,像這樣:

.item-subject::before { 
    content: url([[@{/images/new.png}]]); 
    margin-right: 5px; 
} 

這應該可以解決你的任何問題擁有絕對的網址。

+0

哦不,Spring-Boot不使用Thymeleaf 3 ... – sjngm