一、SpingBoot整合thymeleaf:
1、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
注意添加属性,设定版本,设定版本是为了不再日志中显示thymeleaf的日志.(非必须)
<properties>
<!-- 不再显示thymeleaf的日志 -->
<thymeleaf.version>3.0.0.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.0</thymeleaf-layout-dialect.version>
</properties>
2. 在application.properties或application.xml中进行thymeleaf的配置
spring:
thymeleaf:
prefix: classpath:/templates/
#不使用缓存
cache: false #html具有缓存的特点,这里不用,每次刷新都请求服务器 可选
suffix: .html #可选
mode: HTML5 #可选
encoding: UTF-8 #可选
3. 在resources下面建立templates文件夹,放置html文件,static放置样式、js等文件,webjars使用外部jquery等引用是需创建
至此配置完成,如需,跳转到html过程跟跳转到jsp类似,控制层返回页面名称即可,此处不进行演示
2、热部署,即是在项目中修改代码不用重新启动服务,提高效率。
方法如下:
1.在pom文件中引入 devtools 依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<!-- optional=true, 依赖不会传递, 该项目依赖devtools; 之后依赖boot项目的项目如果想要使用devtools, 需要重新引入 -->
<optional>true</optional>
</dependency>
2.在application.properties 文件中码上以下内容:
spring.thymeleaf.cache=true //缓存
spring.devtools.restart.enabled=true //开启
spring.devtools.restart.additional-paths=src/main/java //监听目录
3.原理
<!-- devtools可以实现页面热部署(即页面修改后会立即生效,
这个可以直接在application.properties文件中配置spring.thymeleaf.cache=false来实现) -->
<!-- 实现类文件热部署(类文件修改后不会立即生效),实现对属性文件的热部署。 -->
<!-- 即devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),
注意:因为其采用的虚拟机机制,该项重启是很快的 -->
<!-- (1)base classloader (Base类加载器):加载不改变的Class,例如:第三方提供的jar包。 -->
<!-- (2)restart classloader(Restart类加载器):加载正在开发的Class。 -->
<!-- 为什么重启很快,因为重启的时候只是加载了在开发的Class,没有重新加载第三方的jar包。 -->