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

南山网站公司山东网站建设开发

南山网站公司,山东网站建设开发,网页制作重庆,wordpress站点地图目录 一、什么是 Thymeleaf 模板引擎 二、Thymeleaf 模板引擎的 Maven 坐标 三、配置 Thymeleaf 四、访问页面 五、访问静态资源 六、Thymeleaf 使用示例 七、Thymeleaf 常用属性 前言 在现代 Web 开发中#xff0c;模板引擎被广泛用于将动态内容渲染到静态页面中。Thy…目录 一、什么是 Thymeleaf 模板引擎 二、Thymeleaf 模板引擎的 Maven 坐标 三、配置 Thymeleaf 四、访问页面 五、访问静态资源 六、Thymeleaf 使用示例 七、Thymeleaf 常用属性 前言 在现代 Web 开发中模板引擎被广泛用于将动态内容渲染到静态页面中。Thymeleaf 是一种流行的模板引擎特别适用于 Spring Boot 项目。它能够在服务器端渲染 HTML 页面同时支持与业务逻辑分离的开发模式提高了开发效率。 一、什么是 Thymeleaf 模板引擎 模板引擎主要用于解决前端显示和后端业务数据的分离。通过模板引擎可以将动态数据填充到静态页面中如 HTML 或 XML 格式从而实现视图与数据的分离。这样不仅提升了开发效率还使得代码更易于重用保持了良好的设计模式。 二、Thymeleaf 模板引擎的 Maven 坐标 在 Spring Boot 项目中使用 Thymeleaf我们需要在 pom.xml 文件中添加以下 Maven 依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-thymeleaf/artifactId /dependencydependencygroupIdnet.sourceforge.nekohtml/groupIdartifactIdnekohtml/artifactIdversion1.9.22/version /dependency三、配置 Thymeleaf 在 application.yml 或 application.properties 文件中我们可以进行一些常见的配置来定制 Thymeleaf 的行为 spring:thymeleaf:cache: false # 关闭页面缓存便于开发时查看更改encoding: UTF-8 # 设置模板编码prefix: classpath:/templates/ # 模板文件所在的目录suffix: .html # 模板文件后缀mode: HTML5 # 设置模板模式为 HTML5四、访问页面 在 Spring Boot 中我们通过 Controller 层来映射请求和返回视图。以下是一个基本的 Controller 示例 import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;Controller public class IndexController {RequestMapping(/)public String index(){return index; // 返回模板名称Thymeleaf 会根据配置寻找 templates/index.html} }五、访问静态资源 在 Spring Boot 中我们可以通过配置来访问静态资源如 CSS、JS 文件、图片等。以下是如何在 application.yml 文件中配置静态资源的访问路径 spring:mvc:static-path-pattern: /static/**在 HTML 页面中引用静态资源时可以使用相对路径 link relstylesheet href../static/css/mystyle.css/此外当访问页面时可能会遇到缺少图标如 favicon.ico的问题报以下错误 在页面头部加入以下代码来解决 link relshortcut icon href../resources/favicon.ico th:href{/static/favicon.ico}/六、Thymeleaf 使用示例 在 Java 控制器中我们可以通过 Model 或 ModelMap 向 Thymeleaf 模板传递数据。以下是一个基本的示例展示了如何向模板传递一个字符串并显示在 HTML 页面中 Controller 层 RequestMapping(/hello) public String hello(Model model){model.addAttribute(msg, Hello);return hello; // 返回模板名称 hello.html }HTML 模板hello.html !DOCTYPE html html langen xmlns:thhttp://www.thymeleaf.org bodyh1Hello/h1div th:text${msg}/div !-- 显示 Hello -- /body /html七、Thymeleaf 常用属性 Thymeleaf 提供了许多属性来简化 HTML 内容的渲染以下是一些常用的属性和示例 1. th:text 和 th:utext th:text设置元素的文本内容并会自动转义 HTML 标签。th:utext设置元素的文本内容但不转义 HTML 标签。 示例 RequestMapping(thymeleaf) public String thymeleaf(ModelMap map){map.put(thText, th:text设置文本内容 b加粗/b);map.put(thUText, th:utext 设置文本内容 b加粗/b);map.put(thValue, thValue 设置当前元素的value值);return thymeleaf; }HTML 模板thymeleaf.html p th:text${thText}/p !-- 输出 th:text设置文本内容 b加粗/b -- p th:utext${thUText}/p !-- 输出 th:utext 设置文本内容 b加粗/bb加粗/b会被渲染为加粗文本 -- input typetext th:value${thValue} !-- 设置输入框的默认值 --2. th:each循环 th:each 用于遍历集合并动态渲染每一项。 示例 RequestMapping(/thymeleaf) public String listUser(Model model) {ListPerson userList new ArrayList();for (int i 0; i 10; i) {userList.add(new Person(i, 张三 i, 20 i, 男));}model.addAttribute(users, userList);return thymeleaf; }HTML 模板thymeleaf.html divulli th:eachuser : ${users}span th:text${user.id}/span-span th:text${user.name}/span-span th:text${user.age}/span-span th:text${user.sex}/span/li/ul /div3. th:if条件判断 th:if 用于根据条件渲染元素。 示例 RequestMapping(/thymeleaf) public String listUser(Model model) {model.addAttribute(size, 3);return thymeleaf; }HTML 模板thymeleaf.html div th:if${size} eq 3div你好/div /divThymeleaf 还支持多种条件判断如 eq等于、gt大于、lt小于等运算符。 总结 通过本文我们了解到 Thymeleaf 的基本使用方法和常用功能包括如何集成到 Spring Boot 项目中如何在模板中渲染动态数据以及如何使用 Thymeleaf 提供的多种标签进行页面渲染。Thymeleaf 提供了一种简洁且强大的方式来处理前端页面渲染尤其适合与 Spring Boot 框架结合使用。 希望本文章对你深入理解 Thymeleaf 模板引擎有所帮助。欢迎随时交流
http://www.ho-use.cn/article/10815519.html

相关文章:

  • 北京网站策划联系电话企业网站的建立
  • godaddy 建网站网站建设登录
  • 武安网站建设山东省建设项目备案证明网站
  • 买卖网站建设易优建站系统
  • 网站建设大wordpress 栏目显示
  • 网站是哪个公司做的好wordpress 登出函数
  • 贵州建网站报价自做视频网站赚钱吗
  • 九江做网站的公司wordpress邮件重置密码
  • 做网站在哪里买空间域名商会小程序开发一个多少钱啊
  • 商务网站建设考试佛山搜索seo优化排名
  • 360推广和百度推广哪个好巩义做网站xd seo
  • 济南区网站开发长沙人才网官网入口
  • 门户网站的营销特点数码类网站名称
  • 怎么上传网站源码网站做百度口碑
  • 广州新建站wordpress怎么编辑
  • 网站服务对生活的影响wordpress视频模板下载
  • 网站建设 宣传网站建设网站模板
  • 网站开发服务合同范本自己专业做网站
  • 王磊网络网站建设定制型网站建设服务
  • 站长工具网站备案厘米售卡站怎么做网站
  • 建设什么网站移动网站如何优化排名
  • 商城建站系统源码花店网站模板 html
  • 网站做语言切换wordpress jquery
  • 网站建设前期准备买一个商标大概要多少钱
  • 河源定制网站建设推广太仓建设工程信息网站
  • 苏州制作公司网站的推广自己的网站
  • 展示型网站方案页面模板如何设置
  • 网站做流量推广的方式贵州城乡住房建设网站
  • 发帖那个网站好 做装修的成都网站建设 外包
  • 如何做视频类网站网络营销企业网站设计