spring boot2 Thymeleaf模板引擎的基本使用

本文介绍如何使用Thymeleaf在HTML页面中动态加载下拉框选项,并实现根据后台返回的数据自动选中相应项。通过Spring Boot项目配置,结合Thymeleaf标签和函数,实现数据的绑定与回显。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Thymeleaf动态加载select下拉框,并选中后台返回的对应类型作为回显

Thyme leaf应该翻译成百里香的叶子(spring boot推荐使用的页面模板,比JSP帅气一些),驼峰命名ThymeLeaf,其中L显得较为突兀,就像ipad或者iphone如果P小写那么显得p腿太长了,不协调,iPad或者iPhone,好了,这是我瞎编的,我这样理解不影响使用,毕竟这个单词查不到,不好读,所以我喜欢拆开方便理解.(标题的重点在最后)

pom依赖(spring-boot-starter-thymeleaf,不然页面404就是家常便饭了哈)

<!--thymeleaf百里香叶页面-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

 配置文件配置(yml也可以)

#thymeleaf配置
#模板的模式,支持如:HTML、XML、TEXT、JAVASCRIPT等
spring.thymeleaf.mode=HTML5
#编码,可不用配置
spring.thymeleaf.encoding=UTF-8
#内容类别,可不用配置
spring.thymeleaf.servlet.content-type=text/html
#开发配置为false,避免修改模板还要重启服务器
spring.thymeleaf.cache=false
#配置模板路径,默认就是templates,可不用配置
#spring.thymeleaf.prefix=classpath:/templates/

常用标签(类似jstl,EL表达式)

th:action  定义后台控制器路径
th:each  循环语句
th:field  表单字段绑定
th:href 定义超链接
th:id  div标签中的id声明,类似html标签中的id属性
th:if  条件判断语句
th:include  布局标签,替换内容到引入文件
th:fragment  布局标签,定义一个代码片段,方便其它地方引用
th:object  替换对象
th:src  图片类地址引入
th:text  显示文本
th:value  属性赋值
常用函数:
#dates  日期函数
#lists  列表函数
#arrays  数组函数
#strings  字符串函数
#numbers  数字函数
#calendars  日历函数
#objects  对象函数
#bools  逻辑函数

 例子(后台方法)

    /**
     * 去添加页时把数据填充到model,渲染模板再把model数据进行渲染把下拉框填充
     * @param model
     * @return
     */
    @RequestMapping("/goAdd")
    public String goAdd(Model model){
        List<Team> teamList = teamService.getTeamByCondition(null);
        System.out.println(teamList);
        List<PlayerRole> playerRoleList = playerRoleService.getPlayerRoleByCondition(null);
        System.out.println(playerRoleList);
        model.addAttribute("teamList",teamList);
        model.addAttribute("playerRoleList",playerRoleList);
        return "addPlayer";
    }

 HTML页面例子(以前写在JSP,现在直接写在HTML,以下拉框为例子,注意HTML头部)

<html xmlns:th="http://www.w3.org/1999/xhtml">
 <div class="layui-form-item">
                <label class="layui-form-label">选择角色</label>
                <div class="layui-input-block">
                    <select name="playerRole" lay-verify="required">
                        <option value=""></option>
                        <!--playerRoleList后台获取到的集合th:each进行遍历,playerRole作为集合对象的变量名称,th:value="${playerRole.id}作为value值
                        th:text="${playerRole.name}"作为需要显示的内容-->
                        <option th:each="playerRole:${playerRoleList}" th:value="${playerRole.id}"
                                th:text="${playerRole.name}"></option>

                    </select>
                </div>
            </div>

 

 

 

说明:我是使用layui提供的模板,js获取数据和表单都是layui的,所以代码只展示重点,最后说明一下,你可以把Thymeleaf理解为可以直接在HTML写Thymeleaf的标签,注意html头部导入xmlns:th="http://www.w3.org/1999/xhtml"就行了

回显需求概述:比如获取到球员的类型是中锋,并选中

 代码th:field="*{playerInfo.playerRole}"是选中的关键

            <div class="layui-form-item">
                <label class="layui-form-label">选择角色</label>
                <div class="layui-input-block">
                    <!--th:field="*{playerInfo.playerRole}" playerInfo表示球员实体,playerRole球员实体的属性,
                    比如 playerRole获得的是2(1:对应前锋,2:中锋,3:后卫),那么就显示中锋,*代表所有的都会匹配显示-->
                    <select name="playerRole" lay-verify="required" th:field="*{playerInfo.playerRole}">
                       <option value=""></option>
                        <!--playerRoleList后台获取到的集合
                        th:each="playerRole:${playerRoleList}"  遍历的写法,相当于foreach,playerRole表示变量(实体),${playerRoleList}表示需要遍历的集合
                        th:value="${playerRole.id}作为value值    ${playerRole.id}球员角色ID作为value值
                        th:text="${playerRole.name}"            作为下拉框需要显示的内容
                        th:each="playerRole:${playerRoleList}"-->
                        <option th:each="playerRole:${playerRoleList}"
                                th:value="${playerRole.id}"
                                th:text="${playerRole.name}"
                                ></option>
                    </select>
                </div>

如有疑问请评论,及时回复.

## springboot整合thymeleaf ### 1. 导入起步依赖 ```xml org.springframework.boot spring-boot-starter-thymeleaf ``` ### 2. 更改引入版本 ```xml 3.0.2.RELEASE 2.1.1 ``` > 1. springboot自带的thymeleaf依赖为2.1.3版本,使用thymeleaf-layout-dialect版本为2以下版本。 > 2. 使用3或3以上的thymeleaf时,需要thymeleaf-layout-dialect的版本为2或以上。 > 3. 锁定thymeleaf版本时不能使用thymeleaf.version标签,会和springboot内部的依赖标签冲突。应当使用springboot-thymeleaf.version标签来锁定版本。 ### 3. 配置文件配置 ```properties spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.check-template-location=true spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.mode=HTML spring.thymeleaf.cache=false ``` > spring.thymeleaf.cache为缓存,需要热部署时,需要设置为false ## 语法 ### 1. 替换标签体内容 ```html 显示欢迎 显示欢迎 ``` ### 2. 替换属性 ```html 显示欢迎 ``` ### 3. 在表达式中访问属性域 ```html 访问属性域 访问请求域 方式一 访问请求域 方式二 访问Session域 访Session域 方式一 访问Application域 方式一 ``` ### 4. 解析url地址 ```html 解析URL地址,获取ContextPath的值 @{}是把ContextPath的值附加到指定的地址前 @{}是把ContextPath的值附加到指定的地址前 ``` ### 5. 直接执行表达式 ```html 直接执行表达式 无转义效果 : [[${attrRequestScope}]] 有转义效果 : [(${attrRequestScope})] ``` ### 6. 分支与迭代 #### 1. if 判断 ```html if判断字符串是否为空 attrRequestScope不为空显示 ~~~attrRequestScope为空显示~~~~ ``` #### 2. 测试循环 ```html 测试循环 ``` > 1. 使用th:each进行集合数据迭代 > 2. th:each="声明变量:${集合}" > 3. th:each 用在哪个标签上,哪个标签就会出现多次 ### 7. 引入外部代码 1. 要被引入的代码 include/part.html ```html 被包含的内容1111 被包含的内容2222 被包含的内容3333 ``` 2. 需要引入的页面 ```html ``` 3. 渲染后的页面源码 ```html 被包含的内容1111 被包含的内容2222 被包含的内容3333 ``` > 1. " :: "左边的值拼前后缀后必须能够找到要包含的文件。 > 2. " :: " 右边的值是代码片段的名字 ,就是th:fragment的值。 > 3. insert将代码原样引入。 > 4. replace使用被引入的代码和属性替换原有的。 > 5. include使用被引入的代码div内部的代码。
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值