springboot集成thymeleaf

120 阅读1分钟

1、pom.xml中添加thymeleaf依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2、application.peoperties文件中配置thymeleaf

#是否启用模板缓存 
spring.thymeleaf.cache = false
#写入HTTP响应的内容类型值
spring.thymeleaf.servlet.content-type = text/html; charset=utf-8
#是否为Web框架启用Thymeleaf视图解析
spring.thymeleaf.enabled = true
#模板文件编码
spring.thymeleaf.encoding = UTF-8
#要应用于模板的模板模式
spring.thymeleaf.mode = HTML5
#在生成URL时预先加上查看名称的前缀。
spring.thymeleaf.prefix = classpath:/templates/
#生成URL时附加到视图名称的后缀
spring.thymeleaf.suffix = .html

3、添加templates视图存放位置

4、编写视图,即html页面

5、controller

  @Controller
  @RequestMapping("/demo")
  public class DemoController {
      @RequestMapping("/thymeleaf")
      public String thymeleaf(Model model) {
          model.addAttribute("name", "Java");
          return "hello";
      }
  }

6、效果