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

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

 package com.bie.config;

 import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /**
*
*/
@Configuration
public class SpringMvcWebConfigSupport implements WebMvcConfigurer { /**
* 默认访问的是首页 //保留了SpringBoot的自动配置,也使用了自己的SpringMmv的配置
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");//前拼templates,后拼.html
registry.addViewController("/index.html").setViewName("index");//浏览器发送/请求来到login.html页面,不用写controller控制层的请求方法了
} /**
* 将static下面的js,css文件加载出来
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//registry.addResourceHandler("/static/").addResourceLocations("classpath:/static/");
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
}

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

 //package com.bie.config;
//
//import org.springframework.context.annotation.Configuration;
//import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
//
///**
// * WebMvcConfigurerAdapter类已经不推荐使用了
// */
//@Configuration
//public class SpringMvcWebConfig extends WebMvcConfigurerAdapter {
//
//// @Override
//// public void addViewControllers(ViewControllerRegistry registry) {
//// //浏览器发送请求到到指定的页面
//// registry.addViewController("/").setViewName("index");
//// }
//
// public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
// WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter(){
// @Override
// public void addViewControllers(ViewControllerRegistry registry) {
// registry.addViewController("/").setViewName("index");
// }
// };
// return adapter;
// }
//}

待续......

SpringBoot2.x|Thymeleaf页面不能正常载入css、js文件的更多相关文章

  1. AJAX载入外部JS文件到页面并让其执行的方法(附源码)

    一. 向HTML页面中动态添加JS文件(从外部载入)并让其执行的两种方法 1.只适用于IE浏览器的简单方法: 先在文档中放置一张JS"空床"并添加ID:<script id= ...

  2. jsp 引用css/js文件返回html网页问题

    我的问题: 我直接在web.xml中匹配了 “/” ,以为能默认使用 “localhost:8080/news/” 这种方式,直接进入首页. 但是这样会匹配所有url 因此请求的 ***.js/*** ...

  3. js活jQuery实现动态添加、移除css/js文件

    下面是在项目中用到的,直接封装好的函数,拿去在js中直接调用就可以实现css.js文件的动态引入与删除.代码如下 动态加载,移除,替换css/js文件 // 动态添加css文件 function ad ...

  4. 在桌面右键创建html,css,js文件

    1.在开始里面输入regedit,进入注册表编辑器. 2.打开HKEY_CLASSES_ROOT项. 3.打开.html/.css/.js项. 4.右键新建项,起名ShellNew. 5.新建字符串值 ...

  5. Django使用本地css/js文件

    Django使用本地css/js文件 在manager.py同层级下创建static文件夹, 里面放上css , js, images等文件或者文件夹 我的文件夹层级 然后只需在settings.py ...

  6. wap开发使用jquery mobile之后页面不加载外部css样式文件/js文件

    场景: wap开发,使用jquery mobile之后不会加载外部自定义的css文件了,需要手动刷新才会加载,查看外部自定义的js文件也是一样. 解决办法: 1.在page下面添加css样式,就不要写 ...

  7. js中动态载入css js样式

    js中动态载入css样式,方法如下: //<link rel="stylesheet" type="text/css" href="http:/ ...

  8. 打开一个网站中的不同页面时,相同的js文件会被重复加载吗?

    作者:JasonYang链接:https://www.zhihu.com/question/41184156/answer/135195798来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非 ...

  9. 关于css js文件缓存问题

    什么情况下,要禁止静态文件缓存:1.经常可能要改动的 js, css.比如一个js文件引用如下<script src="test.js"></script> ...

随机推荐

  1. 使用ZeroClipboard操作剪切板

    一.ZeroClipboard下载地址 点击下载 二.添加js引用 <script src="../Assets/js/jquery-1.8.3.min.js">< ...

  2. JS基础语法---阶段复习+作业练习+接下来知识点heads up

    调试:调试代码---高级程序员都是从调试开始的 调试: 写代码---打开浏览器--F12(开发人员工具)--->Sources---双击文件,在某一行代码前面点击一下(出现的东西就是断点) 一元 ...

  3. RiscV汇编介绍(1)-编译过程

    从c/c++源文件,到可以执行文件,需要以下几个步骤: 预处理/编译 汇编 链接 下面我们以hello world程序为例,展示整个编译链接过程. 1. 编写hello.c代码 #include &l ...

  4. 【转载】Gradle for Android 第三篇( 依赖管理 )

    依赖管理是Gradle最闪耀的地方,最好的情景是,你仅仅只需添加一行代码在你的build文件,Gradle会自动从远程仓库为你下载相关的jar包,并且保证你能够正确使用它们.Gradle甚至可以为你做 ...

  5. ADB控制手机命令(adb命令)

    手机端配置tcp方式连接 su setprop service.adb.tcp.port 5555 stop adbd start adbd 首先使用管理员权限,然后打开监听5555端口 电脑端使用a ...

  6. DevOps 工程师成长日记系列二:配置

    原文地址:https://medium.com/@devfire/how-to-become-a-devops-engineer-in-six-months-or-less-part-2-config ...

  7. java.lang.IllegalArgumentException: Prometheus requires that all meters with the same name have the same set of tag keys.

    创建Spring Cloud Sleuth对应Zipkin服务,引入依赖: <dependency> <groupId>io.zipkin.java</groupId&g ...

  8. [Linux] centos6.5升级安装的supervisor

    因为我要用它来管理多个PHP进程去执行任务,在默认版本下的配置多个子进程不起作用 默认版本的supervisor版本比较低2.1.9,对于运行多个子进程貌似有问题,最新版的supervisor4.1的 ...

  9. excel工资表,转化为word工资单

    1.新建excel表,.xls格式; .xlsx格式的添加到word中会报错‘无法打开数据源’(office 2007): 2.新建word文档.docx格式: 3.邮件——开始邮件合并——信函,选择 ...

  10. Appium滑动函数:Swipe()

    Appium处理滑动方法是swipe 滑动API:Swipe(int start x,int start y,int end x,int y,duration) 解释: int start x-开始滑 ...