SpringBoot with Thymeleaf - css not found

后端 未结 6 1956
没有蜡笔的小新
没有蜡笔的小新 2020-12-06 06:15

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

相关标签:
6条回答
  • 2020-12-06 06:19

    Put your css folder inside resources/static folder

    0 讨论(0)
  • 2020-12-06 06:26

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

    <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.

    0 讨论(0)
  • 2020-12-06 06:29

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

    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. =)

    0 讨论(0)
  • 2020-12-06 06:29

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

    0 讨论(0)
  • 2020-12-06 06:31

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

    .../static/css/app.css
    

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

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

    in both cases the css should be available through

    th:href="@{/css/app.css}"
    
    0 讨论(0)
  • 2020-12-06 06:39

    1. Using Custom Resource Path

    In your Web Config

    @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 views

    <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 the style.css inside 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: You can remove the assets folder 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.

    0 讨论(0)
提交回复
热议问题