SpringBoot2.x | Thymeleaf page can not be loaded css, js files normal

1, to achieve the realization WebMvcConfig configuration page class can solve the problem can not load css, js of;

 1 package com.bie.config;
 2 
 3 import org.springframework.context.annotation.Configuration;
 4 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
 5 import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
 6 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 7 
 8 /**
 9  *
10  */
11 @Configuration
12 public class SpringMvcWebConfigSupport implements WebMvcConfigurer {
13 
14     /**
15      * The default home page is accessed
 16       * @param Registry
 . 17       * / 
18 is      @Override
 . 19      public  void addViewControllers (ViewControllerRegistry Registry) {
 20 is          registry.addViewController ( " / " ) .setViewName ( " index " );
 21 is          registry.addViewController ( " / index.html " ) .setViewName ( " index " );
 22 is      }
 23 is  
24      / * *
 25       * the following static load js, css file out
 26      * @param registry
27      */
28     @Override
29     public void addResourceHandlers(ResourceHandlerRegistry registry) {
30         //registry.addResourceHandler("/static/").addResourceLocations("classpath:/static/");
31         registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
32     }
33 }

Because the new version 2.x SpringBoot in WebMvcConfigurerAdapter (WebMvcConfigurerAdapter can use to extend the functionality SpringMVC) configuration class is no longer recommended for use, you can use WebMvcConfigurer or WebMvcConfigurationSupport to configure your own configuration information.

 1 //package com.bie.config;
 2 //
 3 //import org.springframework.context.annotation.Configuration;
 4 //import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
 5 //import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
 6 //
 7 ///**
 8 // * WebMvcConfigurerAdapter类已经不推荐使用了
 9 // */
10 //@Configuration
11 //public class SpringMvcWebConfig extends WebMvcConfigurerAdapter {
12 //
13 is  /// / @Override
 14  /// / public void addViewControllers (ViewControllerRegistry Registry) {
 15  /// / // browser sends a request to the specified page to
 16  /// / registry.addViewController ( "/"). setViewName ( "index");
 . 17  /// /} 
18 is  // 
. 19  //     public WebMvcConfigurerAdapter webMvcConfigurerAdapter () {
 20 is  //         WebMvcConfigurerAdapter new new WebMvcConfigurerAdapter = Adapter () {
 21 is  //             @Override
 22 is  //             public void addViewControllers (Registry ViewControllerRegistry ) {
 23 //                registry.addViewController("/").setViewName("index");
24 //            }
25 //        };
26 //        return adapter;
27 //    }
28 //}

 

to be continued......

Guess you like

Origin www.cnblogs.com/biehongli/p/11031340.html