1. SpringBoot 静态资源映射规则
-
webjars
:以JAR包的方式引入静态资源; - 所有
/webjars/**
,都去classpath:/META-INF/resources/webjars/
目录下加载资源; -
/**
访问当前项目的任何资源(即静态资源文件夹)classpath:/META-INF/resources/
-
classpath:/resources/
, -
classpath:/static/
, -
classpath:/public/
, -
/
:当前项目的根路径
// WebMvcAutoConfiguration.java
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Integer cachePeriod = this.resourceProperties.getCachePeriod();
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(registry
.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(cachePeriod));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(
registry.addResourceHandler(staticPathPattern)
.addResourceLocations(
this.resourceProperties.getStaticLocations())
.setCachePeriod(cachePeriod));
}
}
2. Thymeleaf 模板引擎
- SpringBoot 引入 Thymeleaf 模板引擎
// pom.xml
<properties>
<!-- 切换 thymeleaf 版本 --
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
// classpath:/templates/, 存放HTML页面
success.html
// 编写Controller
@RequestMapping("/success")
public String success(){
return "success";
}
参考资料: