CSS not loading in Spring Boot

前端 未结 8 2170
渐次进展
渐次进展 2020-12-05 00:12

I am new to spring frame work and spring boot.I am trying to add the static html file with CSS,javascript,js. the file structure is

8条回答
  •  借酒劲吻你
    2020-12-05 00:55

    You need to put your css in /resources/static/css. This change fixed the problem for me. Here is my current directory structure.

    src
      main
        java
          controller
            WebAppMain.java
        resources
          views
            index.html
          static
            css
              index.css
              bootstrap.min.css
    

    Here is my template resolver:

    public class WebAppMain {
    
      public static void main(String[] args) {
        SpringApplication app = new SpringApplication(WebAppMain.class);
        System.out.print("Starting app with System Args: [" );
        for (String s : args) {
          System.out.print(s + " ");
        }
        System.out.println("]");
        app.run(args);
      }
    
    
      @Bean
      public ViewResolver viewResolver() {
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setTemplateMode("XHTML");
        templateResolver.setPrefix("views/");
        templateResolver.setSuffix(".html");
    
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(templateResolver);
    
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(engine);
        return viewResolver;
      }
    }
    

    And just in case, here is my index.html:

    
    
    
        Subscribe
        
        
        
    
            
        
        
    
    
    

    Hello

    Hello World!

提交回复
热议问题