Java 带有 Thymeleaf 的 SpringBoot - 找不到 css

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29562471/
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-11 08:13:20  来源:igfitidea点击:

SpringBoot with Thymeleaf - css not found

javaspring-bootthymeleaf

提问by Bato

First to say is that I've been searching for a solution for a while now and I'm quite desperate now.

首先要说的是,我一直在寻找解决方案有一段时间了,现在我非常绝望。

I cannot get the css file to be accessible from html page when run by Spring Boot.

由 Spring Boot 运行时,我无法从 html 页面访问 css 文件。

html.file

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 lang="en">
        <title th:text='#{Title}'>AntiIntruder</title>
        <meta charset="UTF-8" />
        <link rel="stylesheet" type="text/css" media="all" href="../assets/css/style.css" th:href="@{/css/style.css}" />
    </head>
    <body>
...

Application.java

应用程序.java

@SpringBootApplication // adds @Configuration, @EnableAutoConfiguration, @ComponentScan
@EnableWebMvc
public class Application extends WebMvcConfigurerAdapter {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/*");
    }
}

folder structure:

文件夹结构:

folder structure

文件夹结构

I've tried putting the cssfolder into a staticfolder and/or removing the addResourcesHandlers, referencing to the css by relative path and some other things. Nothing seems to resolve this. Please, let me know also if you tried to solve this but did not find a solution, so that I know, that I'm not ignored.

我尝试将css文件夹放入static文件夹和/或删除 addResourcesHandlers,通过相对路径和其他一些东西引用 css。似乎没有什么能解决这个问题。如果您尝试解决此问题但没有找到解决方案,也请告诉我,以便我知道,我不会被忽视。

采纳答案by Bato

The problem was the @EnableWebMvcannotation in the Application.javafile. As soon as I removed that one, the css started to be available at localhost:8080/css/style.cssbut was not applied. So far I haven't found the reason why the @EnableWebMvcwas causing the problem.

问题是文件中的@EnableWebMvc注释Application.java。一旦我删除了那个,css就开始可用localhost:8080/css/style.css但没有应用。到目前为止,我还没有找到@EnableWebMvc导致问题的原因。

Then I removed a controller mapped to /**that I had implemented in order to display custom error page.

然后我删除了一个映射到/**我已经实现的控制器以显示自定义错误页面。

@RequestMapping("/**")
public String notFound() {
    return "errors/404";
}

After removing also this one, I've got my css working. =)

也删除了这个之后,我的 css 就可以工作了。=)

回答by Sezin Karli

My advice is to put (again) cssfolder under staticfolder, remove addResourcesHandlers and reach css with absolute path (e.g. /css/style.css).

我的建议是将(再次)css文件夹放在static文件夹下,删除 addResourcesHandlers 并使用绝对路径(例如/css/style.css)到达 css 。

回答by MystyxMac

If you put your css in the static folder, you dont need the addResourceHandlers method.

如果将 css 放在 static 文件夹中,则不需要 addResourceHandlers 方法。

.../static/css/app.css

Or if you really want to put them in the assets folder:

或者,如果您真的想将它们放在 assets 文件夹中:

.addResourceLocations("classpath:/assets/") <-- without the * at the end
.../assets/css/app/css

in both cases the css should be available through

在这两种情况下,css 应该可以通过

th:href="@{/css/app.css}"

回答by Faraj Farook

1. Using Custom Resource Path

1. 使用自定义资源路径

In your Web Config

在您的 Web 配置中

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
  if (!registry.hasMappingForPattern("/assets/**")) {
     registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/");
  }
}

Put your style.cssfile inside this folder

将您的style.css文件放在此文件夹中

src/main/resources/assets/css/

src/main/resources/assets/css/

After that in your views

在那之后在你看来

<link rel="stylesheet" type="text/css" th:href="@{/assets/css/style.css}" />

.

.

2. Using predefined paths in spring boot

2.在spring boot中使用预定义的路径

Remove addResourceHandlersfrom your web config

addResourceHandlers从您的网络配置中删除

Put the style.cssinside any of the following folders

style.css里面的任何以下文件夹

  • src/main/resources/META-INF/resources/assets/css
  • src/main/resources/resources/assets/css/
  • src/main/resources/static/assets/css/
  • src/main/resources/public/assets/css/
  • src/main/resources/META-INF/resources/assets/css
  • src/main/resources/resources/assets/css/
  • src/main/resources/static/assets/css/
  • src/main/resources/public/assets/css/

And in the view

并且在视图中

<link rel="stylesheet" type="text/css" th:href="@{/assets/css/style.css}" />

.

.

NOTE: You can remove the assetsfolder here. If you want to do it, remove it from the predefined resource folder and also from the view th:href. But i kept it as it is because, you explicitly mentioned the assets/path in your question. So I belive it's your requirement to have assets/in your resource URL.

注意:您可以在assets此处删除文件夹。如果您想这样做,请将其从预定义的资源文件夹和视图中删除th:href。但我保持原样,因为您assets/在问题中明确提到了路径。所以我相信assets/你的资源 URL 中有它的要求。

回答by L01c

Put your css folder inside resources/staticfolder

将您的 css 文件夹放在resources/static文件夹中

回答by Grant Murphy

For me I had to remove the static reference to the stylesheet for it to work in thymeleaf. So

对我来说,我必须删除对样式表的静态引用才能在 thymeleaf 中工作。所以

<link rel="stylesheet" type="text/css" media="all" href="../assets/css/style.css" th:href="@{/css/style.css}">

Became

成为

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

I spent hours trying all sorts of configurations and file path renaming. Don't know why but this is the only thing that got my css and js to load in Spring Boot 5.

我花了几个小时尝试各种配置和文件路径重命名。不知道为什么,但这是让我的 css 和 js 在 Spring Boot 5 中加载的唯一方法。