thymeleaf依赖_Spring Boot 整合 Thymeleaf

1. 什么是 Thymeleaf

  • Thymeleaf 是新一代的 Java 模板引擎,类似于 Velocity、FreeMarker 等传统引擎,其语言和 HTML 很接近,而且扩展性更高;
  • Thymeleaf 的主要目的是将优雅的模板引入开发工作流程中,并将 HTML 在浏览器中正确显示。同时能够作为静态引擎,让开发成员之间更方便协作开发;
  • Spring Boot 官方推荐使用模板,而且 Spring Boot 也为 Thymeleaf 提供了完整的自动化 配置解决方案;
  • Thymeleaf 使用教程请戳 Tutorial: Using Thymeleaf,配合 Spring 使用的教程请戳 Tutorial: Thymeleaf + Spring

2. 整合过程

2.1 添加 Thymeleaf 依赖

添加 Thymeleaf 依赖有两种方式:

  1. 在新建项目时添加,在 Templeate Engines 中勾选 Thymeleaf;

f624a2570b0e77aba06ecab00795aab6.png
  1. 对于未添加 Thymeleaf 依赖的项目,直接在 pom.xml 中手动添加依赖即可;
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-thymeleaf</artifactId>
 </dependency>

2.2 编写实体类和 Controller

  1. 新建实体类 User
 package com.cunyu.pojo;
 ​
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 import org.springframework.stereotype.Component;
 ​
 /**
  * @author : cunyu
  * @version : 1.0
  * @className : Author
  * @date : 2020/7/29 16:20
  * @description : User 实体类
  */
 ​
 @Component
 @Data
 @AllArgsConstructor
 @NoArgsConstructor
 public class User {
     private int age;
     private String name;
     private String email;
 }
  1. 编写 Controller
 package com.cunyu.controller;
 ​
 import com.cunyu.pojo.User;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.servlet.ModelAndView;
 ​
 /**
  * @author : cunyu
  * @version : 1.0
  * @className : UserController
  * @date : 2020/7/29 16:22
  * @description : UserController
  */
 ​
 @Controller
 public class UserController {
 ​
     // 访问 ip:port/index
     @GetMapping("/index")
     public ModelAndView index() {
         ModelAndView modelAndView = new ModelAndView();
         // 设置跳转的视图
         modelAndView.setViewName("index");
         modelAndView.addObject("title", "Thymeleaf 使用");
         modelAndView.addObject("desc", "Spring Boot 整合 Thymeleaf");
 ​
         User author = new User(25, "村雨遥", "747731461@qq.com");
 ​
         modelAndView.addObject("author", author);
         return modelAndView;
     }
 }

2.3 创建Thymeleaf 模板

第 2.3 中,设置了跳转的视图为 index,所以我们需要在 src/main/resources/templates 中创建 index.html

 <!DOCTYPE html>
 <html xmlns="http://www.w3.org/1999/xhtml"
       xmlns:th="http://www.thymeleaf.org">
 <head>
     <meta charset="UTF-8">
     <title th:text="${title}"></title>
     
 </head>
 <body>
 <h1 th:text="${desc}" th:align="center"></h1>
 <h2 th:align="center">=====作者信息=====</h2>
 <p th:text="${author?.name}"></p>
 <p th:text="${author?.age}"></p>
 <p th:text="${author?.email}"></p>
 </body>
 </html>
 ​

2.4 测试

启动项目,然后在浏览器中访问 http://localhost:8080/index,如果出现下图中的信息,说明整合成功。

477181d41b0e027112ae5a95613da09a.png

3. 注意事项

为了方便使用,我们在使用 Thymeleaf 模板时,可以添加一些自己的配置;

 # thymelea模板配置
 ​
 # 设置模板文件存放位置
 spring.thymeleaf.prefix=classpath:/templates/
 # 设置模板后缀
 spring.thymeleaf.suffix=.html
 # 语法严格限制
 spring.thymeleaf.mode=HTML5
 # 编码格式
 spring.thymeleaf.encoding=UTF-8
 # 热部署,每次修改静态页面都不用重启就可以生效,默认为 true
 spring.thymeleaf.cache=false

f648f083d60361822c614133a1a4182b.png


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ThymeleafSpring Boot整合非常简单,只需要在pom.xml中添加Thymeleaf依赖,然后在Spring Boot的配置类中添加Thymeleaf的配置即可。 以下是一个简单的ThymeleafSpring Boot整合示例: 1.在pom.xml文件中添加Thymeleaf依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 2.在Spring Boot的配置类中添加Thymeleaf的配置: ``` @Configuration public class ThymeleafConfig { @Bean public ViewResolver viewResolver() { ThymeleafViewResolver resolver = new ThymeleafViewResolver(); resolver.setTemplateEngine(templateEngine()); resolver.setCharacterEncoding("UTF-8"); return resolver; } @Bean public SpringResourceTemplateResolver templateResolver() { SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); templateResolver.setPrefix("classpath:/templates/"); templateResolver.setSuffix(".html"); templateResolver.setTemplateMode(TemplateMode.HTML); return templateResolver; } @Bean public SpringTemplateEngine templateEngine() { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver()); return templateEngine; } } ``` 3.创建一个Thymeleaf模板: 在src/main/resources/templates目录下创建一个名为"index.html"的文件,并添加以下内容: ``` <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Hello Thymeleaf</title> </head> <body> <h1 th:text="'Hello ' + ${name} + '!'" /> </body> </html> ``` 4.创建一个Spring Boot控制器: ``` @RestController public class MyController { @GetMapping("/") public String index(Model model) { model.addAttribute("name", "World"); return "index"; } } ``` 5.运行应用程序: 现在可以运行应用程序,并访问http://localhost:8080/,应该可以看到一个包含"Hello World!"的页面。在这个示例中,Thymeleaf的模板引擎将"Hello World!"渲染到了模板中的h1元素中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值