【发布时间】:2021-01-17 14:51:46
【问题描述】:
经过大量研究,我无法找到解决方案。我有一个使用 Spring Boot + Spring Security + Thymeleaf 实现的项目。
我有一个 REST API 多模块项目和一个使用 Thymeleaf 构建的 Web 客户端。我的问题是我似乎无法在我的 Thymeleaf 页面中包含我的 CSS 样式表。
MvcConfig :
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.
addResourceHandler(
"/css/**",
"/img/**").
addResourceLocations(
"classpath:/resources/static/img",
"classpath:/resources/static/css");
}
我的 Html (Thymeleaf)
<!DOCTYPE HTML >
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head xmlns:th="http://www.thymeleaf.org">
<title>Bibliothèque d'OC</title>
<link rel="stylesheet" type="text/css" th:href="@{/static/css/style.css}">
<!-- CSS only -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<!-- JS, Popper.js, and jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
</head>
我尝试更改在link 标记(th:href="@{/static/css/style.css}") 的th:href 属性上设置的路径值。而且我还尝试将href 属性添加到link 标签;没有人帮忙。我找不到解决方案。
有人可以提出解决方案吗?
【问题讨论】:
-
„…我试图改变我的路径…“ – @Jqk3 – 你指的是什么“路径”?你的代码 sn-p 到底在哪里? — „……或添加一个 href……” — 同样的问题。你对什么“添加一个href”? TIA。
-
我这样尝试: 改变这个:th:href="@{/static/css/style.css}"
-
„…然后改变这个:th:href="@{/static/css/style.css}"…“ - 你改变了什么 来自?也就是说,它最初是什么?或者,如果您的回复是指该属性最初,那么您将
th:href="@{/static/css/style.css}"更改为的什么? -
我刚试了几个网址
-
„我刚刚尝试了几个网址“ - 你试过the suggestions I've proposed中的任何一个吗?你的项目的目录结构是什么?这是一个非常重要的细节,你还没有说清楚。您的回复可以采用以下格式:„
…/src/blah/foo/bar/…“。 TIA。
标签: css spring spring-boot spring-security thymeleaf