2017-09-18 169 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