Spring Boot+Thymeleaf css不应用于模板
我正在评估从模板生成pdf的Thymeleaf和飞碟,我在将css应用到我的Thymeleaf模板时遇到了问题。我已经阅读了相关问题和答案,并且;但是没有一个建议的解决方案解决了我的问题 以下是“我的资源”文件夹的外观: 因此,我使用Spring将查找的默认目录。这就是头标记在my template.html中的外观: 我使用WebContext而不是Context,因为我在Context中遇到以下错误:Spring Boot+Thymeleaf css不应用于模板,css,spring-boot,thymeleaf,Css,Spring Boot,Thymeleaf,我正在评估从模板生成pdf的Thymeleaf和飞碟,我在将css应用到我的Thymeleaf模板时遇到了问题。我已经阅读了相关问题和答案,并且;但是没有一个建议的解决方案解决了我的问题 以下是“我的资源”文件夹的外观: 因此,我使用Spring将查找的默认目录。这就是头标记在my template.html中的外观: 我使用WebContext而不是Context,因为我在Context中遇到以下错误: 我在这里遗漏了什么,为什么我的style.css没有应用于template.html?语法
我在这里遗漏了什么,为什么我的style.css没有应用于template.html?语法看起来不错,所以问题不在于语法
此外,如果没有IWebContext接口,您也无法使用@{…}语法,因此您会遇到此异常。我遇到了类似的问题-我的css未应用于我的模板页面 我的问题是css文件是css sass格式的
.table
margin: 0 0 40px 0
当我把它转换成正常的css格式
.table {
margin: 0 0 40px 0;
}
它成功了我也遇到了同样的问题,我还尝试使用thymeleaf模板解析器生成pdf。我对thymeleaf和spring框架做了很多研究,我尝试了WebContext,我尝试了HttpServletRequest,我尝试了一些spring thymeleaf集成解决方案,但它也不起作用。所以我认为这不是语法错误,我最终使用了绝对路径而不是相对路径。 这里是我假设的原因,假设我们的资源是在localhost:8080/myapp/css/style.css上提供的。请求资源的相对路径实际上取决于它所关联的上下文。 对于EAX示例,一个普通的thymeleaf模型Veiw,它在客户端的浏览器上以html页面的形式返回,因此这种情况下的上下文将是请求主机名、端口和应用程序上下文xteg:localhost:8080/myapp。相对路径将以此为基础。所以,如果相对路径是/css/style.css,那么context+相对路径的结果将是localhost:8080/myapp/css/style.css 与web上下文不同,在我们的例子中,脱机模板位于服务器后端,因此我假设的上下文是运行上下文的服务器,它是本地服务器路径+appcontexteg:D:/myServer/apps/myapp,相对路径/css/style.css在这里是D:/myServer/apps/myapp/css/style.css,这没有意义。为了使用静态资源,我必须通过它的绝对路径 我开始使用:
<link rel="stylesheet" type="text/css" th:href="@{http://localhost:8080/myapp/css/style.css}"/>
从HttpServletRequest获取基本url,您可以将其插入到方法中或自动连接到服务类中,或者从RequestContextHolder获取。我在我的服务课上写下:
private static String getCurrentBaseUrl() {
ServletRequestAttributes sra = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest req = sra.getRequest();
return req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + req.getContextPath();
}
这是我在课堂上使用模板引擎的地方:
Context context = new Context();
context.setVariable("variales", variables);
context.setVariable("baseUrl", getCurrentBaseUrl());
String content = springTemplateEngine.process("myTemplate",context);
在我的模板中,我使用如下绝对css url:
<link type="stylesheet" th:src="@{|${baseUrl}/css/style.css|}" />
我通过改变href中的路径结构解决了这个问题。我的目录结构和你的一样html文件在模板文档中,css文件在静态文档中
<head>
<title>Spring Boot and Thymeleaf Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" href="/css/style.css"/>
</head>
它可能会帮助您将css应用到html页面。我还认为语法和资源的结构都很好。内联css也可以工作,所以我认为pdf生成没有问题。这就是为什么我感到困惑,我不知道问题出在哪里。谢谢你的回答。我也可以使用绝对路径,尽管我不太喜欢使用绝对路径。不过有一句话:我在模板中使用了th:href=@{baseUrl}/css/style.css},不需要实现addResourceHandlers。我想我们的情况是一样的,我也不喜欢绝对路径。但据我所知,这是唯一能让它为我工作的方法。对于资源处理程序,我只想确保静态内容必须在web上提供。但这并不是那么糟糕,因为基本url完全取决于真实的用户请求,所以它仍然是灵活的。如果你能找到相对路径的解决方案,请让我知道。非常感谢。
private static String getCurrentBaseUrl() {
ServletRequestAttributes sra = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest req = sra.getRequest();
return req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + req.getContextPath();
}
Context context = new Context();
context.setVariable("variales", variables);
context.setVariable("baseUrl", getCurrentBaseUrl());
String content = springTemplateEngine.process("myTemplate",context);
<link type="stylesheet" th:src="@{|${baseUrl}/css/style.css|}" />
<head>
<title>Spring Boot and Thymeleaf Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" href="/css/style.css"/>
</head>