zoukankan      html  css  js  c++  java
  • SpringBoot2.x|Thymeleaf页面不能正常载入css、js文件

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

    扩展SpringMvc,编写一个配置类(@Configuration),是WebMvcConfigurationAdapter抽象类类型(WebMvcConfigurer 接口类型的),且不能标注@EnableWebMvc
    如果SpringBoot本身的自动配置不能满足自己的需求,就需要扩展SpringMVC配置文件。WebMvcConfigurer可以扩展SpringMvc的功能。

     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      * 默认访问的是首页 //保留了SpringBoot的自动配置,也使用了自己的SpringMmv的配置
    16      * @param registry
    17      */
    18     @Override
    19     public void addViewControllers(ViewControllerRegistry registry) {
    20         registry.addViewController("/").setViewName("index");//前拼templates,后拼.html
    21         registry.addViewController("/index.html").setViewName("index");//浏览器发送/请求来到login.html页面,不用写controller控制层的请求方法了
    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 //}

    待续......

  • 相关阅读:
    SQL Prompt 5.3.4.1
    RIA(富客户端)发展态势
    XML操作:2.LINQ TO XML(http://www.cnblogs.com/AlexLiu/archive/2008/10/27/linq.html)
    XML操作:1.XML类(http://blog.csdn.net/happy09li/article/details/7460521)
    .NET的Snk使用方法
    PictureEdit中拖放图片
    CPU与内存(经典问答)
    SQL Server 2008 Data Types and Entity Framework 4
    8086、80x86(IA-32)、64(IA-64)位CPU发展
    MVC3 Razor模板引擎
  • 原文地址:https://www.cnblogs.com/biehongli/p/11031340.html
Copyright © 2011-2022 走看看