Refers to a .css file with a thimeleaf in spring mvc

I have been doing the project since spring MVC and Thymeleaf. I have a question about how I should link to my CSS files if I have this folder structure:

src main webapp resources myCssFolder myCssFile.css web-inf spring views myViewFolder index.html 

My configuration class looks like this:

 @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/**"); } 

And I call href in my index file as follows:

 href="resources/css/bootstrap.min.css" 

But there are some elements that are confused on my page, for example, CSS does not work.

+6
source share
2 answers

You will need to use the th:href attribute to access the css files. Here is an example from a thimeleaf textbook. If the thymelear cannot evaluate the value of th:href , it defaults to 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> 
+9
source

I have such a problem! Here are the steps that helped me.

  1. I have a directory /resources/css/myCSS.css. So I put css in the root, for example /css/myCSS.css, and deleted the / resources directory
  2. I bind MyCSS as follows:

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

+3
source

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


All Articles