Springboot thymeleaf语法的理解和使用

这是我对thymeleaf语法的理解,现在与大家分享一下,首先是介绍thymeleaf的用法如下步骤:
一:在springboot项目 pom文件中导入以下两个依赖:
在这里插入图片描述

二:导入依赖后就是在HTML页面顶部html标签中添加一句代码就可以在了也就是xmlns:th=http://www.thymeleaf.org这一句代码然后就可以在HTML中使用thymeleaf语法了。
Thymeleaf语法主要是与控制器联合使用,如果你想在控制器那边传入一些变量那么就可以在HTML中直接使用,主要是在标签上添加th:xxx 就可以了如HTML中的代码:

<div th:text="${msg}"></div>

这样就调用了控制器传过来的数据,接着就看看控制器是如何传数据的如:

@Controller
public class IndexController {
    @RequestMapping("/test")
    public String test(Model model){
        model.addAttribute("msg","<h1>hello,springboot</h1>");
        return "test";
    }
}

这里就是控制器的代码使用的是model类来传数据的,启动页面页面就会显示<h1>hello,springboot</h1>着句代码,如果想显示里面标签的样式的话就需要把HTML中的th:text改成th:utext就可以是显示h1标签样式的hello,springboot的字体了,这就是thymeleaf的使用,接下来就是说说thymeleaf的样式,他都是以th:xxx来设置标签的样式的而接收的有以下几种:
1、 以${xxx}接收的是一些自定义的变量如上面就是一些自定义的属性变量;
2、 以#{xxx}接收的就是国际话消息;
3、 以@{xxx}接收的是路径,也就是一些链接。
4、 如果是片段表达式的话就是用~{xxx}去取值
等等,以上这几个就是thymeleaf样式的取值。那么接下来就是讲讲我对thymeleaf的语法了解了。
如:th:text就是文本取值不排除标签 ;th:utext就是排除标签样式以外取文本th:each就是for循坏等等。。以上就是我对springboot thymeleaf的初步理解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot Thymeleaf项目是使用Spring Boot框架和Thymeleaf模板引擎来构建的。在配置Spring Boot Thymeleaf项目时,需要进行以下几个步骤: 1. 在pom.xml文件中引入Thymeleaf依赖。可以使用以下代码片段将Thymeleaf依赖添加到pom.xml文件中: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 这将确保项目中包含Thymeleaf所需的所有依赖项。\[2\] 2. 在application.properties或application.yml文件中添加Thymeleaf的配置。可以使用以下代码片段来配置Thymeleaf的相关属性: ``` spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.cache=false spring.thymeleaf.suffix=.html spring.thymeleaf.servlet.content-type=text/html ``` 这些配置将指定Thymeleaf模板文件的位置、编码方式、缓存设置等。\[1\] 3. 在项目中使用Thymeleaf模板引擎来创建和渲染页面。可以使用Thymeleaf语法和标签来编写动态的HTML页面。在Spring Boot中,可以将Thymeleaf模板文件放置在src/main/resources/templates目录下,并使用@Controller注解的控制器类来处理请求并返回Thymeleaf模板。\[1\] 通过以上步骤,你就可以开始使用Spring Boot和Thymeleaf来构建动态的Web应用程序了。 #### 引用[.reference_title] - *1* *2* [springBoot整合thymeleaf(超简单)](https://blog.csdn.net/baobei_ll/article/details/124213027)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [springboot使用thymeleaf](https://blog.csdn.net/qq_41378597/article/details/85644813)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值