探索Thymeleaf:用动态Web模板引擎打造吸引人的用户界面(SpringBoot的html详解)

什么是Thymeleaf?

Thymeleaf是一个用于Web和独立环境的现代服务器端Java模板引擎,用于处理XML/XHTML/HTML5内容。它特别适合基于Spring框架的Web应用程序,因为它提供了与Spring MVC的出色集成。Thymeleaf以其自然的模板语法和强大的数据绑定能力而闻名,使得开发者能够轻松地创建动态Web页面。

Thymeleaf的特点

  • 自然模板技术:Thymeleaf的模板语法尽可能接近HTML,使得模板易于阅读和编写。
  • 强大的数据绑定:支持多种数据绑定方式,包括标准表达式和选择表达式,使得数据展示更加灵活。
  • 安全性:自动处理HTML转义,防止XSS攻击。
  • 与Spring的集成:无缝集成Spring MVC,支持Spring的表达式语言(SpEL)。
  • 高性能:通过缓存和优化的模板处理机制,提供快速的页面渲染速度。

Thymeleaf示例

以下是一个简单的Thymeleaf模板示例,展示了如何使用Thymeleaf来动态生成Web页面内容。

html 复制代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
        "http://www.w3.org/TR/html4/loose.dtd">  
<html lang="en" xmlns:th="http://www.thymeleaf.org">  
<head>  
    <title>Thymeleaf示例</title>  
    <!-- 样式省略 -->  
</head>  
<body>  
    <!-- 使用th:utext和th:id进行文本替换和ID设置 -->  
    <div th:utext="${hello}" th:id="${hello.toUpperCase()}">xxxx</div>  
  
    <!-- 直接从模型属性中获取值并填充到输入框 -->  
    <input th:value="${user.getUsername()}">  
  
    <!-- 使用th:object和*{}语法访问对象属性 -->  
    <div th:object="${user}">  
        <span th:text="*{username}"></span>  
    </div>  
  
    <!-- 条件渲染 -->  
    <a th:href="www.csdn.net" th:if="${user.getAge() == 2}">年龄</a>  
  
    <!-- 根据条件设置类名(注释掉的代码) -->  
    <!-- <a th:class="${user.getAge() > 2}?'class1':'class2'">年龄</a> -->  
  
    <!-- 成绩等级判断 -->  
    <p th:if="${user.score >= 60 and user.score < 85}">B</p>  
    <p th:if="${user.score < 60}">C</p>  
    <p th:if="${user.score > 85}">优秀</p>  
  
    <!-- 性别判断 -->  
    <span th:switch="${user.gender}">  
        <p th:case="1">男</p>  
        <p th:case="2">女</p>  
    </span>  
  
    <!-- 表格数据展示 -->  
    <table>  
        <tr>  
            <td>姓名</td>  
            <td>密码</td>  
            <td>序号</td>  
        </tr>  
        <tr th:each="a,aState:${uList}">  
            <td th:text="${a.username}"></td>  
            <td th:text="${a.password}"></td>  
            <td th:text="${aState.index}"></td>  
        </tr>  
    </table>  
</body>  
</html>

Controller部分

java 复制代码
package com.qcby.demo.controller;

import com.qcby.demo.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/hello")
    public String hello(HttpServletRequest req, HttpSession httpSession, Model model){
        model.addAttribute("hello","<h1>renliang</h1>");
        User user = new User();
        user.setPassword("111");
        user.setUsername("renliang");
        user.setAge(2);
        user.setScore(78);
        user.setGender(2);
        List<User> uList = new ArrayList<>();
        for (int i = 0; i < 10; i++){
            User u = new User();
            u.setUsername("renliang"+i);
            u.setPassword("111"+i);

            uList.add(u);
        }

        // httpSession.setAttribute("user", user);
        model.addAttribute("user", user);
        model.addAttribute("uList", uList);
        return "user";
    }
}
相关推荐
2601_962106648 小时前
【Java Set综合实战】商品管理系统 + 图书收藏系统|Product、Book自定义类全套代码
java
RuoyiOffice8 小时前
SpringBoot+Vue3 凭证生命周期设计:审核、过账、冲销与反过账怎样保持账目一致
java·spring boot·事务·springboot3·财务系统·会计凭证·ruoyi office
2601_962106649 小时前
HashMap入门|工号-姓名键值存储、两种遍历方式、根据key/value查
java
卸任9 小时前
Vibe coding工具使用心得
前端·ai编程
杨凯凡9 小时前
Spring Framework 7 API 版本管理实战:请求头路由、兼容迁移与废弃策略
java·rest api·springframework·spring boot 4·api版本管理
brien910 小时前
为什么模型标称 1M,Google Antigravity 聊到 256K 却突然失忆?我们给它做了个原生行内监控圆环
前端
T_Wizard10 小时前
同一个问题写了 4 遍:官方 SDK / HTTP / Spring AI / LangChain4j,到底该选哪个
java·ai编程
hai_android10 小时前
AudioTrack 深度解析:从原理到实战
android·java·kotlin
修炼的dance10 小时前
录屏关键帧间隔多少合适?六档实测,2秒到10秒小了75.6%
前端
Lank_M10 小时前
关键帧从1秒拉到10秒,录屏小87.3%,摄像头小10.8%
前端