SpringBoot2.x|Thymeleaf页面不能正常载入css、js文件
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文件的更多相关文章
- AJAX载入外部JS文件到页面并让其执行的方法(附源码)
一. 向HTML页面中动态添加JS文件(从外部载入)并让其执行的两种方法 1.只适用于IE浏览器的简单方法: 先在文档中放置一张JS"空床"并添加ID:<script id= ...
- jsp 引用css/js文件返回html网页问题
我的问题: 我直接在web.xml中匹配了 “/” ,以为能默认使用 “localhost:8080/news/” 这种方式,直接进入首页. 但是这样会匹配所有url 因此请求的 ***.js/*** ...
- js活jQuery实现动态添加、移除css/js文件
下面是在项目中用到的,直接封装好的函数,拿去在js中直接调用就可以实现css.js文件的动态引入与删除.代码如下 动态加载,移除,替换css/js文件 // 动态添加css文件 function ad ...
- 在桌面右键创建html,css,js文件
1.在开始里面输入regedit,进入注册表编辑器. 2.打开HKEY_CLASSES_ROOT项. 3.打开.html/.css/.js项. 4.右键新建项,起名ShellNew. 5.新建字符串值 ...
- Django使用本地css/js文件
Django使用本地css/js文件 在manager.py同层级下创建static文件夹, 里面放上css , js, images等文件或者文件夹 我的文件夹层级 然后只需在settings.py ...
- wap开发使用jquery mobile之后页面不加载外部css样式文件/js文件
场景: wap开发,使用jquery mobile之后不会加载外部自定义的css文件了,需要手动刷新才会加载,查看外部自定义的js文件也是一样. 解决办法: 1.在page下面添加css样式,就不要写 ...
- js中动态载入css js样式
js中动态载入css样式,方法如下: //<link rel="stylesheet" type="text/css" href="http:/ ...
- 打开一个网站中的不同页面时,相同的js文件会被重复加载吗?
作者:JasonYang链接:https://www.zhihu.com/question/41184156/answer/135195798来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非 ...
- 关于css js文件缓存问题
什么情况下,要禁止静态文件缓存:1.经常可能要改动的 js, css.比如一个js文件引用如下<script src="test.js"></script> ...
随机推荐
- swoole 内存泄露的问题有没有好的办法解决
在传统的web开发模式中,我们知道,每一次php请求,都要经过php文件从磁盘上读取.初始化.词法解析.语法解析.编译等过程,而且还要与nginx或者apache通信,如果再涉及数据库的交互,还要再 ...
- JS常用标签
1.由来 JavaScript的出现就是为了解决,不需要将所有的表单数据全部提交到服务器. 2.添加 加载Js代码的三种方式: 第一种:<script></script>标签里 ...
- Linux(ubuntu) 一行代码搞定查看文件目录
ls 命令:• ls 是英文单词 list 的简写,其功能为列出目录的内容,是用户最常用的命令之一,类似于 DOS 下的 dir 命令 ls命令之后加各种参数的作用: ls -a 显示指定目录下所有子 ...
- BayaiM__MySQL 常用函数
BayaiM__MySQL 常用函数 原创 作者:bayaim 时间:2016-06-16 09:11:13 122 0删除编辑 MySQL 常用函数 阅读(883430) | 评论(44543) ...
- Mysql—主从复制
https://www.jb51.net/article/137925.htm https://blog.51cto.com/13706760/2171361 https://www.cnblogs. ...
- Linux:目录的查找
搜索文件与目录 find [查找范围] [查找条件表达式] 常用的选项 -name 按名称查找,允许使用通配符 -type 按文件类型查找 文件类型包括: 普通文件 f 目录 d 块设备文件 b 字符 ...
- 2、mongoDB的基本操作
数据写入和查询: show dbs (查看有哪些数据库) use imooc (使用数据库) db.dropDatabase() 删除数据库 备注:在use的时候如果use一个不存在的表,在mongo ...
- luoguP3071 [USACO13JAN]座位Seating
https://www.luogu.org/problem/P3071 AC代码: https://www.luogu.org/blog/user33426/solution-p3071 莫名其妙RE ...
- C++ 标准库 std::find 查找
参见:https://en.cppreference.com/w/cpp/algorithm/find 查找指定字符/数字等. #include <iostream> #include & ...
- python-使用skimage显示图片
import skimage.io import skimage.transform import matplotlib.pyplot as plt img = skimage.io.imread(' ...