SpringBoot2.x|Thymeleaf页面不能正常载入css、js文件

1、实现实现WebMvcConfig配置类可以解决页面不能加载css,js的问题;

 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      * 默认访问的是首页
16      * @param registry
17      */
18     @Override
19     public void addViewControllers(ViewControllerRegistry registry) {
20         registry.addViewController("/").setViewName("index");
21         registry.addViewController("/index.html").setViewName("index");
22     }
23 
24     /**
25      * 将static下面的js,css文件加载出来
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 }

因为在SpringBoot的2.x新版本中WebMvcConfigurerAdapter (使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能)配置类已经不推荐使用了,可以使用WebMvcConfigurer 或者WebMvcConfigurationSupport来配置自己的配置信息。

 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 ////    @Override
14 ////    public void addViewControllers(ViewControllerRegistry registry) {
15 ////        //浏览器发送请求到到指定的页面
16 ////        registry.addViewController("/").setViewName("index");
17 ////    }
18 //
19 //    public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
20 //        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter(){
21 //            @Override
22 //            public void addViewControllers(ViewControllerRegistry registry) {
23 //                registry.addViewController("/").setViewName("index");
24 //            }
25 //        };
26 //        return adapter;
27 //    }
28 //}

待续......

猜你喜欢

转载自www.cnblogs.com/biehongli/p/11031340.html
今日推荐