知识点1,webjars方式引入第三方公共资源
使用jar包形式引入资源,网址如下:https://www.webjars.org/
这里举例使用jquery
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.5.1</version>
</dependency>
使用
知识点2,第三方公共资源放入指定文件夹
├── WebMvcAutoConfiguration
│ ├── addResourceHandlers
│ │ ├── this.resourceProperties.getStaticLocations()
│ │ │ ├── staticLocations
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/" };
这里我们新建一个public目录,存放js
html资源
hello.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<script src="./js/jquery-3.5.1.min.js">
</script>
</head>
<body>
<h1>使用 jQuery 输出 Hello World</h1>
<p>1234</p>
</body>
<script type="text/javascript">
$(document).ready(function(){
$("p").text("js/jquery-3.5.1.min.js");
});
</script>
</html>
知识点3,默认首页
根据源代码,取名为index.html即可
private Optional<Resource> getWelcomePage() {
String[] locations = getResourceLocations(this.resourceProperties.getStaticLocations());
return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
}
private Resource getIndexHtml(String location) {
return this.resourceLoader.getResource(location + "index.html");
}
知识点4,资源默认路径切换
结合配置文件ResourceProperties,配置即可
spring.resources.static-locations=classpath:/res
知识点5,Thymeleaf配置
结合配置文件ThymeleafProperties,配置即可