SpringBoot之Thymeleaf 语法详解=>尾部添加整合thymeleaf示例

本文详细介绍了SpringBoot中Thymeleaf的使用,包括变量输出与字符串操作、日期格式化处理、条件判断、迭代遍历、域对象操作以及URL表达式的运用。讲解了th:text、th:value、th:if、th:each等关键语法,并给出了实际示例。

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

===>尾部添加整合thymeleaf示例

1.变量输出与字符串操作

1.th:text 

在页面中输出值

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

2.th:value

可以将一个值放入到 input 标签的 value 中

<input type="text" th:value="zhangzq" >

3. 输出 html 片断 th:utext

    <div th:utext="${model.html}" >

    </div>

4.字符串操作

Thymeleaf 内置对象 注意语法: 1,调用内置对象一定要用# 2,大部分的内置对象都以 s 结尾 strings、numbers、dates 

${#strings.isEmpty(key)}   ===>   判断字符串是否为空,如果为空返回 true,否则返回 false
${#strings.contains(msg,'T')}    ===>   判断字符串是否包含指定的子串,如果包含返回 true,否则返回 false
${#strings.startsWith(msg,'a')}    ===>   判断当前字符串是否以子串开头,如果是返回 true,否则返回 false
${#strings.endsWith(msg,'a')}    ===>   判断当前字符串是否以子串结尾,如果是返回 true,否则返回 false
${#strings.length(msg)}   ===>   返回字符串的长度
${#strings.indexOf(msg,'h')}   ===>    查找子串的位置,并返回该子串的下标,如果没找到则返回-1
${#strings.substring(msg,13)} ,{#strings.substring(msg,13,15)}    ===>    截取子串,用户与 jdkString 类下 SubString 方法相同
${#strings.toUpperCase(msg)}, ${#strings.toLowerCase(msg)}    ===>   字符串转大小写。

 

2.日期格式化处理

${#dates.format(key)}    ===>   格式化日期,默认的以浏览器默认语言为格式化标准
${#dates.format(key,'yyy/MM/dd')}    ===>   按照自定义的格式做日期转换
${#dates.year(key)} ${#dates.month(key)} ${#dates.day(key)} year     ===>    取年 Month:取月 Day:取日

 

3.条件判断

1.th:if

<span th:if="${sex} == '男'">
    性别:男
</span>
<span th:if="${sex} == '女'">
    性别:女
</span>

2.th:switch

<div th:switch="${id}">
    <span th:case="1">ID 为 1</span>
    <span th:case="2">ID 为 2</span>
    <span th:case="3">ID 为 3</span>
</div>

4.迭代遍历

1.th:each

<table class="table" >
    <tr th:each=" name : ${arr} " ><td th:text="${name}" ></td></tr>
</table>

后台代码:

@RequestMapping("/thymeleaf")
public String freemarker(Model model){

    List<String> arr = new ArrayList<>();
    arr.add("licm");
    arr.add("zhangzq");
    arr.add("springboot");

    model.addAttribute("msg","zhangzq");
    model.addAttribute("sex","男");
    model.addAttribute("id","1");
    model.addAttribute("arr",arr);
    return "thy/thymeleaf";
}

2.th:each  状态变量
状态变量属性

1,index:当前迭代器的索引 从 0 开始

2,count:当前迭代对象的计数 从 1 开始

3,size:被迭代对象的长度

4,even/odd:布尔值,当前循环是否是偶数/奇数 从 0 开始

5,first:布尔值,当前循环的是否是第一条,如果是返回 true 否则返回 false

6,last:布尔值,当前循环的是否是最后一条,如果是则返回 true 否则返回 false

<table class="table" >
    <tr>
        <th>name</th>
        <th>Index</th>
        <th>Count</th>
        <th>Size</th>
        <th>Even</th>
        <th>Odd</th>
        <th>First</th>
        <th>lase</th>
    </tr>
    <tr th:each=" name , var : ${arr} " >
        <td th:text="${name}" ></td>
        <td th:text="${var.index}"></td>
        <td th:text="${var.count}"></td>
        <td th:text="${var.size}"></td>
        <td th:text="${var.even}"></td>
        <td th:text="${var.odd}"></td>
        <td th:text="${var.first}"></td>
        <td th:text="${var.last}"></td>
    </tr>
</table>

运行效果:

 

3.th:each 迭代 Map

<table class="table" >
    <tr>
        <th>键值对</th>
        <th>值</th>
    </tr>
    <tr th:each="maps : ${map}">
        <td th:text="${maps}"></td>
        <td th:each="entry:${maps}"  th:text="${entry.value }" ></td>
    </tr>
</table>

5.域对象操作

1.HttpServletRequest

<p>Request:<span th:text="${#httpServletRequest.getAttribute('name')}"></span><br/></p>

2.HttpSession

<p>Session:<span th:text="${session.sess}"></span><br/></p>

3.ServletContext

<p>Application:<span th:text="${application.app}"></span></p>

6.URL 表达式

th:href

th:src
1.url 表达式语法

基本语法:@{}
2.URL 类型

A.绝对路径 

<a th:href="@{http://www.baidu.com}">绝对路径</a><br/>
<a href="http://www.baidu.com">绝对路径</a><br/>

B.相对路径

1.相对于当前项目的根 相对于项目的上下文的相对路径 

<a th:href="@{/freemarker}">相对路径</a><br/>

2. 相对于服务器路径的根

<a th:href="@{~/hello/freemarker}">相对于服务器的根</a>

3.在 url 中实现参数传递

1.一般传参

<a th:href="@{/freemarker(username=pkusoft,pwd=123456)}">相对路径</a><br/>

2.rest风格传参

<a th:href="@{/freemarker/{pwd}/(username=pkusoft,pwd=123456)}">rest风格</a><br/>

 

SpringBoot 整合 thy

1.pom配置


        <!-- thy 支持 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2.controller编写

    @RequestMapping("/thy")
    public String thy(Model model){

        model.addAttribute("title","thy");
        model.addAttribute("desc","desc");
        model.addAttribute("name","zhangzq");

        return "index";
    }

注解使用 @Controller

3.springboot 默认使用 thy,模板默认放在 resource 下的 templates 目录下,

现在我们创建一个 html

然后访问  127.0.0.1:8080/thy  就ok了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值