SpringBoot整合Thymeleaf

为何使用Thymeleaf

如果希望以 Jar 形式发布模块则尽量不要使用 JSP 相关知识,这是因为 JSP 在内嵌的 Servlet 容器上运行有一些问题 (内嵌 Tomcat、 Jetty 不支持 Jar 形式运行 JSP,Undertow 不支持 JSP)。

ThymeLeaf常用表达式、标签和函数

  1. 常用表达式
• ${...}变量表达式。
•* { .. . } 选择表达式。
• #{...}消息文字表达式。
• @ {} 链接url 表达式。
• #maps 工具对象表达式。
  1. 常用标签
• th:action 定义后台控制器路径。
• th:each 1,盾环语-句。
• th:field 表单字段绑定。
• th:href 定义超链接。
• th:id div 标签中的ID 声明,类似HTML 标签中的归属性。
• th:if 条件判断语句。
• th:include 布局标签,替换内容到引入文件。
• th :企agment 布局标签,定义一个代码片段,方便其他地方引用。
• th:object 替换对象。
• th:src 图片类地址引入。
• th:text 显示文本。
• th:value 属性赋值。
  1. 常用表达式
• #dates 日期函数。
• #lists 列表函数。
• #arrays 数组函数。
• #strings 字符串函数。
• #numbers 幸生字函捷生。
• #ca lendars 日历函数。
• #objects 对象函数。
• #bools 逻辑函数。

SpringBoot整合Thymeleaf

添加依赖

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

填写thymeleaf配置文件

#Thymeleaf配置
#开发配置为false,避免修改模版还要重启服务器
spring.thymeleaf.cache=false
#编码 可不配置
spring.thymeleaf.encoding=utf-8
#模版的模式,支持HTML、XML、TEXT、JAVASCRIPT
spring.thymeleaf.mode=HTML5
#模版路径,默认是templates,可以不用配置
spring.thymeleaf.prefix=classpath:/templates/
#后缀
spring.thymeleaf.suffix=.html

index2.html页面
一般放在templates文件夹下

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<!--字符串输出-->
<p th:text="'hello SpringBoot'">hello thymeleaf</p>
<!--数学运算-->
<p th:text="9 + 10"></p>
<p th:text="10 * 10"></p>
<p th:text="1 - 10"></p>
<p th:text="8 / 3"></p>
<p th:text="3 % 2"></p>
<!--操作域对象-->
<p th:text="${user}"></p>
<p th:text="${user.username}"></p>

<!--遍历数组-->
<table th:each="item, sta:${user.hobbies}">
    <tr>
        <td th:text="${item}"></td>
        <td th:text="${sta.index}"></td>
        <td th:text="${sta.odd}"></td>
        <td th:text="${sta.size}"></td>
    </tr>
</table>


<!--遍历map-->
<div th:each="item:${user.secrets}" th:text="${item.key}"></div>

<!--遍历数组-->
<div th:each="item:${user.hobbies}" th:text="${item}"></div>

<!--设置属性-->
<input type="text" th:attr="value=${user.username}">
<input type="text" th:attr="value=${user.username}, title=${user.username}">

<!--表单数据绑定-->
<form action="" th:object="${user}">
    <input type="text" th:value="*{username}">
    <input type="password" th:value="*{password}">
    <select>
        <option th:each="item:${user.secrets}" th:text="${item.value}" th:selected="'a' eq ${item.value}"></option>
    </select>
</form>

<!--解析html文本内容-->
<p th:utext="'<span>html</span>'"></p>

<!--流程控制-->
<p th:if="${user.username} != ${user.password}">yes</p>
<div th:switch="${user.username}">
    <p th:case="rose">name is rose</p>
    <p th:case="jack">name is jack</p>
</div>

<!--外部引入-->
<link rel="stylesheet" th:href="@{/css/index.css}">
<script th:src="@{/js/index.js}"></script>

<a th:href="@{/home}">home</a>
</body>

</html>

注意: 使用Thymeleaf要声明名称空间:

xmlns:th="http://www.thymeleaf.org"

Controller层

@Controller
@RequestMapping("user")
public class UserController {

    @RequestMapping("/list")
    public String getList(ModelMap map){
        List<User> list=new ArrayList<User>();
        for(int i=0;i<5;i++){
            User u=new User();
            u.setUsername("xiaxx"+i);
            u.setPassword("mm"+i);
            list.add(u);
        }
        map.addAttribute("list",list);
        return "index";
    }
    @GetMapping("home")
    public String index(Model model) {
        User user = new User();
        user.setUsername("jack");
        user.setPassword("112233");
        user.setHobbies(Arrays.asList(new String[]{"singing", "dancing", "football"}));
        Map<String, String> maps = new HashMap<>();
        maps.put("1", "o");
        maps.put("2", "g");
        maps.put("3", "a");
        maps.put("4", "j");
        user.setSecrets(maps);
        model.addAttribute("user", user);
        return "index2";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot整合Thymeleaf是一种常见的做法,用于在Spring Boot应用中利用Thymeleaf作为模板引擎,提供动态网页功能。Thymeleaf是一个强大的、现代的Web模板引擎,支持HTML5和XML。 以下是整合步骤: 1. 添加依赖:在你的`pom.xml`文件中添加Thymeleaf及其Spring Boot支持的依赖: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> ``` 2. 配置视图解析器:在`application.properties`或`application.yml`中设置Thymeleaf的视图位置: ``` spring.thymeleaf.views.location=classpath:/templates/ ``` 3. 创建模板目录:在项目的`src/main/resources/templates`目录下创建HTML模板文件。 4. 使用Thymeleaf标签:在模板文件中,你可以使用Thymeleaf的表达式语言(EL)和特殊语法,如条件语句、迭代等。 ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>My Spring Boot App</title> </head> <body> <h1 th:text="${message}">Hello, World!</h1> </body> </html> ``` 5. 在Controller中返回模型数据并指定视图:例如: ```java import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @GetMapping("/") public String home(Model model) { model.addAttribute("message", "Welcome to Spring Boot with Thymeleaf!"); return "home"; // 指定模板名称 } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值