Springboot with Thymeleaf - css not found

First of all, I want to say that I was looking for a solution for a while, and now I am desperate.

I can not access the css file from the html page when starting Spring Boot.

html.file

<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

 @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 tried putting the css folder in the static folder and / or removing addResourcesHandlers, referring to css in a relative path and some other things. Nothing seems to allow this. Please let me know if you try to solve this, but did not find a solution, so that I know that they are not ignoring me.

+6
source share
5 answers

The problem was the @EnableWebMvc annotation in the Application.java file. As soon as I removed this, css became available in localhost:8080/css/style.css , but was not applied. So far, I have not found the reason why the @EnableWebMvc problem caused the problem.

Then I removed the controller mapped to /** , which I implemented to display the user error page.

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

After uninstalling this as well, I have my css working. =)

+3
source

1. Using a custom resource path

In your web configuration

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

Put your style.css file inside this folder

src/main/resources/assets/css/

After that in your submissions

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

.

2. Using predefined paths in spring boot

Remove addResourceHandlers from your web config

Put style.css in any of the following folders

  • 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. Here you can delete the assets folder. If you want to do this, delete it from the predefined resource folder, as well as from the th:href view. But I saved it like this because you explicitly mentioned the assets/ path in your question. Therefore, I believe in your requirement to have assets/ in the URL of your resource.

+21
source

If you put your css in a static folder, you do not need the addResourceHandlers method.

 .../static/css/app.css 

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

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

in both cases css should be accessible via

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

My advice is to put (again) the css folder in the static folder, delete addResourcesHandlers and reach css with an absolute path (e.g. /css/style.css ).

0
source

Put your css folder inside resources / static folder

0
source

Source: https://habr.com/ru/post/984951/


All Articles