SpringBoot with Thymeleaf - css not found

后端 未结 6 1965
没有蜡笔的小新
没有蜡笔的小新 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: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

    
    

    .

    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

    
    

    .

    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.

提交回复
热议问题