当前位置: 首页 > news >正文

探索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页面内容。

<!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部分

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";}
}

http://www.lryc.cn/news/414399.html

相关文章:

  • 视频教程 - 自研Vue3 Tree组件高级功能:虚拟滚动新增节点实现自动滚动
  • 职业生涯阶段总结3:转眼毕业三年
  • 项目经理面试总结
  • (免费领源码)java#springboot#mysql大学校园旧物捐赠网站 25109-计算机毕业设计项目选题推荐
  • Java 设计模式之单例模式
  • Linux系统驱动(二)字符设备驱动
  • Day29 | 动态规划 509. 斐波那契数 70. 爬楼梯 746. 使用最小花费爬楼梯
  • 【开源移植】MultiButton_小型按键驱动模块移植
  • 【Python系列】Python 字典合并
  • C# 设计模式之装饰器模式
  • 【uniapp离线打包】(基于Android studio)
  • 稳稳的年化10%,多任务时序动量策略——基于pytorch的深度学习策略(附python代码)
  • C++分析AVL树
  • aurora8b10b ip的使用(framing接口下的数据回环测试)
  • 如何通过OpenCV判断图片是否包含在视频内?
  • 大数据基础:Spark重要知识汇总
  • Executable Code Actions Elicit Better LLM Agents
  • 循环结构(三)——do-while语句
  • pandas 或筛选
  • 工具(1)—截屏和贴图工具snipaste
  • 【从零开始一步步学习VSOA开发】快速体验SylixOS
  • Ansible自动化:简化IT基础设施管理的艺术
  • 【Rust光年纪】探索Rust语言中的WebSocket库和框架:优劣一览
  • HTML 基础结构
  • 多页合同怎么盖骑缝章_电子合同怎么盖骑缝章?
  • GD 32 IIC通信协议
  • Spring Task初学
  • 决策树可解释性分析
  • BUGKU-WEB never_give_up
  • hive自动安装脚本