SpringBoot中使用模版引擎Thymeleaf语法

SpringBoot中使用模版引擎Thymeleaf语法

​ 以前前端交给我们的页面时 html 然后我们需要转换成jsp页面来实现页面的组装 ,当我们将框架换成springboot 时,springboot推荐我们使用模版引擎 如:Thymeleaf、freemarker等。

第一步我们要在项目中导入jar包:

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

模版引擎的作用:就是取得数据并加以处理,最后显示出数据,可以让(网站)程序实现界面与数据分离,业务代码与逻辑代码分离,从而提升开发效率

第二步:当我们导入jar包完成后,在你的配置文件(application.yml)中添加如下配置

spring:
  thymeleaf:
  	# 是否使用缓存
    cache: false
    # 默认编码格式
    encoding: UTF-8
    # 使用的模型是什么
    mode: HTML5
    # 配置文件前缀
    prefix: classpath:/templates/
    # 配置文件后缀
    suffix: .html

第三步:要在配置文件的基础上,在项目的resources文件夹下的templates中添加模版页面,并在配置文件中引入如下代码,来使用Thymelaf

<html xmlns:th="http://www.thymeleaf.org">

th 语法

关键字功能描述案例
th:id替换id<input th:id="'xxx' + ${collect.id}"/>
th:text文本替换/转义<p th:text="${collect.description}">description</p>
th:utext支持html的文本替换/不转义<p th:utext="${htmlcontent}">conten</p>
th:object替换对象<div th:object="${session.user}">
th:value属性赋值<input th:value="${user.name}" />
th:with变量赋值运算<div th:with="isEven=${prodStat.count}%2==0"></div>
th:style设置样式th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''"
th:onclick点击事件th:onclick="'getCollect()'"
th:each属性遍历tr th:each="user,userStat:${users}">

springboot项目默认查找文件位置:

  • /src/java/resources/static
  • /src/java/resources/public
  • /src/java/resources/templates
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot,你可以使用Thymeleaf作为模板引擎来处理视图层的渲染。Thymeleaf是一种基于HTML的模板引擎,它允许你在HTML模板嵌入动态数据和逻辑处理。 要在Spring Boot使用Thymeleaf,你需要遵循以下步骤: 1. 添加Thymeleaf依赖:在你的项目构建文件(例如Maven的pom.xml或Gradle的build.gradle),添加Thymeleaf的依赖项。 对于Maven项目,添加以下依赖: ```xml dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 对于Gradle项目,添加以下依赖: ```groovy implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' ``` 2. 创建Thymeleaf模板文件:在src/main/resources/templates目录下创建你的HTML模板文件。使用Thymeleaf语法来嵌入动态数据和逻辑。 示例模板文件(index.html): ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Spring Boot Thymeleaf Example</title> </head> <body> <h1>Welcome, <span th:text="${name}"></span>!</h1> </body> </html> ``` 在这个示例,我们使用Thymeleaf语法`th:text="${name}"`将`name`变量的值插入到HTML文档。 3. 创建控制器:创建一个Spring MVC控制器来处理请求并返回Thymeleaf视图。 示例控制器: ```java @Controller public class HomeController { @GetMapping("/") public String home(Model model) { model.addAttribute("name", "John"); return "index"; } } ``` 在这个示例,我们使用`Model`对象将`name`属性添加到模型,并将其传递给`index`视图。 这样,当你访问根路径("/")时,将会渲染`index.html`模板并显示"Welcome, John!"。 这只是一个简单的示例,Thymeleaf还提供了很多强大的功能,比如迭代、条件渲染、表单处理等。你可以参考Thymeleaf官方文档以了解更多详细信息。 希望这能帮助你了解如何在Spring Boot使用Thymeleaf作为模板引擎!如果你有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值