本节将和大家一起实战Spring Boot 2.0 和thymeleaf 模板引擎
1. 创建项目
2. 使用Spring Initlizr 快速创建Spring Boot 应用程序
3. 填写项目配置信息
4. 添加Web 模块
5. 添加thymeleaf 模块
6. 项目保存路径
7. POM.xml 添加thymeleaf依赖
4.0.0 com.xingyun srping-boot-with-thymeleaf-sample 0.0.1-SNAPSHOT jar srping-boot-with-thymeleaf-sample Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
8. 配置application.properties
spring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=.htmlspring.thymeleaf.mode=HTMLspring.thymeleaf.encoding=UTF-8spring.thymeleaf.servlet.content-type=text/html# 关闭缓存,即时刷新,上线生产环境需改成truespring.thymeleaf.cache=false
9. 创建模板文件
index.html
Title this is index.html
welcome.html
Title this is welcome.html
10.创建Controller
package com.xingyun.srpingbootwiththymeleafsample;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HomeController { @GetMapping(value = "/") public String index(){ return "/views/index"; } @GetMapping(value = "/welcome") public String welcome(){ return "/views/welcome"; }}
11. 访问
12. 访问