Thymeleaf + CSS+SpringBoot

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41586841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 12:20:23  来源:igfitidea点击:

Thymeleaf + CSS+SpringBoot

cssspring-bootthymeleaf

提问by Luca Sepe

I have a problem with CSS and Thymeleaf.

我的 CSS 和 Thymeleaf 有问题。

In my Spring boot app, I have this structure:

在我的 Spring Boot 应用程序中,我有这样的结构:

  • src/main/resource/static/css (for css files)
  • src/main/resource/static/templates (for html file)
  • src/main/resource/static/css(用于 css 文件)
  • src/main/resource/static/templates(用于 html 文件)

Now, with my html page named ErrorPage and css file named Layout.css, using Thymeleaf I have, in the head of ErrorPage:

现在,使用名为 ErrorPage 的 html 页面和名为 Layout.css 的 css 文件,在 ErrorPage 的头部使用 Thymeleaf:

<link href="../css/Layout.css" th:href="@{css/Layout.css}" type="text/css" />

But this does not work.

但这不起作用。

What am I doing wrong?

我究竟做错了什么?

回答by DimaSan

Move your templatefolder right under resources:

将您的template文件夹移动到以下位置resources

  • src/main/resource/static/css(for CSS files);
  • src/main/resource/templates(for HTML templates).
  • src/main/resource/static/css(对于 CSS 文件);
  • src/main/resource/templates(对于 HTML 模板)。

Then correct the linktag as follows:

然后link按如下方式更正标签:

<link href="../static/css/Layout.css" th:href="@{/css/Layout.css}" rel="stylesheet" />

回答by Adrian

Move your template folder right under resources:

将您的模板文件夹移动到资源下:

src/main/resources/static/css (for CSS files);
src/main/resources/templates (for HTML templates).

Then correct the link tag as follows (relative or absolute):

然后按如下方式更正链接标签(相对或绝对):

<link href="../css/firstcss.css" rel="stylesheet">
<link href="/css/secondcss.css" rel="stylesheet">

The old solution with static in front doesn't work for me.

前面带有静态的旧解决方案对我不起作用。

回答by David Becerra Montellano

I had the same problem, my css no load found!

我有同样的问题,我的 css 没有找到负载!

I moved my css into folder resources/static/css But it does not work...

我将我的 css 移动到文件夹 resources/static/css 但它不起作用......

Then check my controller and I modified the following:

然后检查我的控制器,我修改了以下内容:

@RequestMapping(value="/{reader}", method=RequestMethod.GET)
// @RequestMapping(value = "/{reader}", method = RequestMethod.GET)

I just retired the method and value spaces...

我刚刚退休了方法和价值空间......

Regards.

问候。