tmplates
下放html
文件- 一个html文件如果要使用thymeleaf语法,需要引入thymeleaf名称空间
<html lang="en" xmlns:th="http://www.thymeleaf.org">
static
下放静态资源
一、引入依赖
org.springframework.boot » spring-boot-starter-thymeleaf » 2.1.8.RELEASE
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
热启动
页面经常要修改,添加了热启动后不用每次都能重启项目来使修改生效,只需要重新编译html文件 ctrl+shift+F9
或者重新编译项目 ctrl+F9
,修改即可生效
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
二、配置
开发时关闭缓存,上线后打开缓存
application.properties
spring.thymeleaf.cache=false
三、测试
tmplates
下新建index.html
,启动项目,访问项目,访问到index.html
因为不带任何访问路径时,springboot默认访问到index.html
若想访问http://127.0.0.1:8080/index.html也访问到index.html
,需要在controller里配置,访问其他html
文件时,也是按同样方法配置
@Controller
public class IndexController {
@RequestMapping({"/", "/index", "/index.html"})
public String index() {
return "index";
}
}
如果访问不到static下的静态资源,可以加这段代码
@Configuration
public class SpringMvcSupport implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
}
}