![](/img/trans.png)
[英]Reference a .css file with thymeleaf in spring mvc using th:href
[英]reference a .css file with thymeleaf in spring mvc
我正在用 spring MVC 和 Thymeleaf 做一个项目。 如果我有这个文件夹结构,我有一个关于我应该如何引用我的 CSS 文件的问题:
src
main
webapp
resources
myCssFolder
myCssFile.css
web-inf
spring
views
myViewFolder
index.html
我的配置类是这样的:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/css/**");
registry.addResourceHandler("/img/**").addResourceLocations("/img/**");
registry.addResourceHandler("/js/**").addResourceLocations("/js/**");
registry.addResourceHandler("/sound/**").addResourceLocations("/sound/**");
registry.addResourceHandler("/fonts/**").addResourceLocations("/fonts/**");
}
我在我的索引文件中调用href
如下:
href="resources/css/bootstrap.min.css"
但是有一些元素在我的页面中有点混乱,例如 CSS 不起作用。
您将需要使用th:href
属性来引用 css 文件。 这是 thymeleaf 教程中的示例。 如果 thymeleaf 无法计算th:href
值,则默认为href
值。
<head>
<title>Good Thymes Virtual Grocery</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" media="all"
href="../../css/gtvg.css" th:href="@{/css/gtvg.css}" />
</head>
我有这样的问题!这些步骤帮助了我。
<link th:href="@{/css/MyCSS.css}" href="/css/MyCSS.css" rel="stylesheet" type="text/css" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.