Spring Boot对静态资源的映射规则
1.Spring Boot源码中关于Spring MVC的配置都在WebMVCAutoConfiguration中
其中addResourceHandlers中的“/webjars/**”都去 "classpath:/META-INF/resource/webjars/"这个路径下找静态资源
webjars以jar包的方式引入静态资源
https://www.webjars.org/从这里https://www.webjars.org/引入webjar依赖
我们这里导入jquery依赖到pom. xml
<引入jquery-webjars>在访问时直接写webjars下面的静态资源名称即可 <dependency> <groupId>org.webjars.npm</groupId> <artifactId>jquery</artifactId> <version>3.3.1</version> </dependency>
通过http://localhost:8080/webjars/jquery/3.3.1/dist/jquery.js可以访问到webjars的静态资源
webjars其中还有一个resurceProperties
@ConfigurationProperties(prefix = "spring resources", ignoreUnknownFields = false)
public class ResourceProperties implements ResourceLoaderAware{
// 可以设置和静态资源有关的参数,缓存时间等等...
2."/**"访问当前项目的任何资源
当没有得到处理时,会默认到以下路径下去查找
(存放静态资源的文件,都是在resouces目录下)
“classpath:/META-INF/resources”
“classpath:/resources”
“classpath:/static”
“classpath:/public”
访问resource/asserts/js/Chart.min.js静态文件
http://localhost:8080/asserts/js/Chart.min.js
3.欢迎页:静态资源文件夹下的所有index.html页面,被“/**”映射时
在静态资源文件夹下创建Html File-index文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>$Title$</title>
</head>
<body>
<h1>首页</h1>
</body>
</html>
访问localhost:8080 输出“主页”
4.所有的**/favicon.ico都在静态资源文件夹下找,直接把需要的图标放在resource目录下
模板引擎
模板+数据 →模板引擎→我们想要的样子
引入thymeleaf
<dependency>
<groundId>org.springFramework.boot</groundId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--更改版本--> <properties> <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version> <!--布局功能的支持程序 thymeleaf3主程序 匹配 layout2以上版本--> <!--thymeleaf2 匹配 layout1--> <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version> </properties>