SpringBoot——Thymeleaf的表达式

本文介绍了Thymeleaf模板引擎中的标准变量表达式、选择变量表达式以及URL表达式的使用。通过示例展示了如何在HTML页面中访问Controller传来的数据,包括使用${}

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

1.Thymeleaf中的标准变量表达式(掌握)

标准变量表达式用于访问容器(tomcat)上下文环境中的变量,功能和 EL 中的 ${} 相同。Thymeleaf 中的变量表达式使用 ${变量名} 的方式获取 Controller 中 model 其中的数据。

语法:${...}

2.Thymeleaf中的选择变量表达式(了解,不推荐使用)

选择变量表达式,也叫星号变量表达式,使用 th:object 属性来绑定对象。

选择表达式首先使用 th:object 来绑定后台传来的 User 对象,然后使用 * 来代表这个对象,后面 {} 中的值是此对象中的属性。

选择变量表达式 *{...} 是另一种类似于标准变量表达式 ${...} 表示变量的方法。

选择变量表达式在执行时是在选择的对象上求解,而${...}是在上下文的变量 Model 上求解,这种写法比标准变量表达式繁琐,只需要大家了解即可。

语法:*{...}

3.案例演示

首先写一个model。

package com.songzihao.springboot.model;

/**
 *
 */
public class User {
    private Integer id;
    private String name;
    private Integer age;

    //getter and setter
}

然后是我们的控制层,UserController。

package com.songzihao.springboot.controller;

import com.songzihao.springboot.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 */
@Controller
public class UserController {

    @RequestMapping(value = "/user/detail")
    public ModelAndView userDetail() {
        ModelAndView mv=new ModelAndView();

        User user=new User();
        user.setId(1001);
        user.setName("张起灵");
        user.setAge(21);

        mv.addObject("user",user);
        mv.setViewName("userDetail");
        return mv;
    }

}

下来写这个 /user/detail 请求对应的html页面。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h2>使用标准变量表达式 ${} 展示User用户信息:</h2>
    用户编号:<span th:text="${user.id}"></span><br />
    用户姓名:<span th:text="${user.name}"></span><br />
    用户年龄:<span th:text="${user.age}"></span><br />
    <hr />
    <h2>使用选择变量表达式 *{} 展示User用户信息(星号表达式,仅在 div 范围内有效)</h2>
    <div th:object="${user}">
        用户编号:<span th:text="*{id}"></span><br />
        用户姓名:<span th:text="*{name}"></span><br />
        用户年龄:<span th:text="*{age}"></span><br />
    </div>
</body>
</html>

最后在核心配置文件中,添加以下内容。

#关闭Thymeleaf缓存
spring.thymeleaf.cache=false

#thymeleaf 模版前缀,默认值,可选项
spring.thymeleaf.prefix=classpath:/templates/
#thymeleaf 模版后缀,默认值,可选项
spring.thymeleaf.suffix=.html

最后启动项目入口类进行测试。

package com.songzihao.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

可以看到我们在这里成功的通过Thymeleaf模板引擎中的变量表达式获取到了后台的数据。


4.Thymeleaf中的URL表达式

主要用于链接、地址的展示,可用于<script src="...">、<link href="...">、<a href="...">、<form action="...">、<img src="">等,可以
在 URL 路径中动态获取数据。

语法:@{...}

4.1 案例演示

首先写一个Controller控制层,里面定义几个请求方法。

package com.songzihao.springboot.controller;

import com.songzihao.springboot.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 */
@Controller
public class UserController {

    @RequestMapping(value = "/url")
    public String urlExpression(Model model) {
        model.addAttribute("name","张起灵");
        model.addAttribute("id",1001);
        model.addAttribute("age",21);
        return "url";
    }

    @RequestMapping(value = "/test")
    public @ResponseBody String test(Integer id,String name,Integer age) {
        return "请求路径/test,获取到的数据中 id = " + id + ", name = " + name + ", age = " + age;
    }

}

然后对应的是我们的html页面。

注意URL表达式中,分别用到了绝对路径和相对路径。(这里推荐使用相对路径)

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>URL路径表达式</title>
</head>
<body>
    <h2>URL路径表达式:@{......}</h2>
    <h4>a标签中的绝对路径(没有参数)</h4><br/>
    <a th:href="@{http://www.tencent.com}">跳转到腾讯</a><br/><br/>
    <a th:href="@{http://localhost:8080/user/detail}">跳转到/user/detail</a><br/><br/>
    <hr/>
    <h4>a标签中的相对路径(没有参数)</h4><br/>
    <a th:href="@{/user/detail}">跳转到/user/detail</a><br/><br/>
    <hr/>
    <h4>a标签中的绝对路径(带参数)</h4><br/>
    <a th:href="@{'http://localhost:8080/test?name=' + ${name}}">查看用户姓名</a><br/><br/>
    <hr/>
    <h4>a标签中的相对路径(带参数)</h4><br/>
    <a th:href="@{'/test?id=' + ${id} + '&name=' + ${name} + '&age=' + ${age}}">复杂写法:查看用户姓名</a><br/><br/>
    <a th:href="@{/test(id=${id},name=${name},age=${age})}">简练写法:查看用户姓名</a>
</body>
</html>

下面是核心配置文件中的内容、以及启动入口类测试。

#关闭Thymeleaf缓存
spring.thymeleaf.cache=false

#thymeleaf 模版前缀,默认值,可选项
spring.thymeleaf.prefix=classpath:/templates/
#thymeleaf 模版后缀,默认值,可选项
spring.thymeleaf.suffix=.html
package com.songzihao.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

点击第一个链接,会跳转至腾讯官网首页。

点击第二、三个链接,会跳转至 userDetail.html 这个页面展示从后台获取到的数据。

点击最后三个链接,会将获取到的用户信息内容进行展示。(这里展示结果我就不再给出截图了)

## 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内部的代码。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贝尔摩德苦艾酒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值