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

低价高端网站设计seo流量的提升的软件

低价高端网站设计,seo流量的提升的软件,泰安网络诈骗案件,喜迎二十大作文目录 一、什么是 Thymeleaf 模板引擎 二、Thymeleaf 模板引擎的 Maven 坐标 三、配置 Thymeleaf 四、访问页面 五、访问静态资源 六、Thymeleaf 使用示例 七、Thymeleaf 常用属性 前言 在现代 Web 开发中,模板引擎被广泛用于将动态内容渲染到静态页面中。Thy…

目录

一、什么是 Thymeleaf 模板引擎

二、Thymeleaf 模板引擎的 Maven 坐标

三、配置 Thymeleaf

四、访问页面

五、访问静态资源

六、Thymeleaf 使用示例

七、Thymeleaf 常用属性


前言

        在现代 Web 开发中,模板引擎被广泛用于将动态内容渲染到静态页面中。Thymeleaf 是一种流行的模板引擎,特别适用于 Spring Boot 项目。它能够在服务器端渲染 HTML 页面,同时支持与业务逻辑分离的开发模式,提高了开发效率。

一、什么是 Thymeleaf 模板引擎

        模板引擎主要用于解决前端显示和后端业务数据的分离。通过模板引擎,可以将动态数据填充到静态页面中,如 HTML 或 XML 格式,从而实现视图与数据的分离。这样,不仅提升了开发效率,还使得代码更易于重用,保持了良好的设计模式。

二、Thymeleaf 模板引擎的 Maven 坐标

在 Spring Boot 项目中使用 Thymeleaf,我们需要在 pom.xml 文件中添加以下 Maven 依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency><dependency><groupId>net.sourceforge.nekohtml</groupId><artifactId>nekohtml</artifactId><version>1.9.22</version>
</dependency>

三、配置 Thymeleaf

application.ymlapplication.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 rel="stylesheet" href="../static/css/mystyle.css"/>

此外,当访问页面时,可能会遇到缺少图标(如 favicon.ico)的问题,报以下错误:

在页面头部加入以下代码来解决:

<link rel="shortcut icon" href="../resources/favicon.ico" th:href="@{/static/favicon.ico}"/>

 

六、Thymeleaf 使用示例

在 Java 控制器中,我们可以通过 ModelModelMap 向 Thymeleaf 模板传递数据。以下是一个基本的示例,展示了如何向模板传递一个字符串并显示在 HTML 页面中:

Controller 层

@RequestMapping("/hello")
public String hello(Model model){model.addAttribute("msg", "Hello");return "hello";  // 返回模板名称 "hello.html"
}

HTML 模板(hello.html)

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body><h1>Hello</h1><div th:text="${msg}"></div>  <!-- 显示 "Hello" -->
</body>
</html>

七、Thymeleaf 常用属性

Thymeleaf 提供了许多属性来简化 HTML 内容的渲染,以下是一些常用的属性和示例:

1. th:textth: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>加粗</b>",<b>加粗</b>会被渲染为加粗文本 -->
<input type="text" th:value="${thValue}">  <!-- 设置输入框的默认值 -->

2. th:each(循环)

  • th:each 用于遍历集合,并动态渲染每一项。
示例:
@RequestMapping("/thymeleaf")
public String listUser(Model model) {List<Person> 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)
<div><ul><li th:each="user : ${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>
</div>

3. 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 3"><div>你好</div>
</div>

Thymeleaf 还支持多种条件判断,如 eq(等于)、gt(大于)、lt(小于)等运算符。

总结

        通过本文,我们了解到 Thymeleaf 的基本使用方法和常用功能,包括如何集成到 Spring Boot 项目中,如何在模板中渲染动态数据,以及如何使用 Thymeleaf 提供的多种标签进行页面渲染。Thymeleaf 提供了一种简洁且强大的方式来处理前端页面渲染,尤其适合与 Spring Boot 框架结合使用。

希望本文章对你深入理解 Thymeleaf 模板引擎有所帮助。欢迎随时交流!

http://www.khdw.cn/news/37910.html

相关文章:

  • 网站备案需要拍照seo广州工作好吗
  • 武汉网站设计师培训学校阳江seo
  • 南昌网站建设行业现状火蝠电商代运营公司
  • 昆明网站快照优化公司灰色词快速排名接单
  • 怎么做直播网站刷弹幕百度售后服务电话
  • 做网站需要找什么客户网站权重怎么看
  • 重庆网站建设开发云南疫情最新数据消息中高风险地区
  • wordpress默认界面做网站seo推广公司
  • 南通网站的优化seo竞价排名
  • 互联网门户网站建设管理 总结中国有几个搜索引擎
  • 网站的建设与预算建网站平台
  • 做网站的如何增加电话量怎么给公司做网站推广
  • 专门做app的原型网站济南seo优化
  • 资源优化网站排名郑州网站优化seo
  • 做一家公司网站要注意哪些品牌推广与传播方案
  • 做图片格式跟尺度用哪个网站好怎么制作自己公司网站
  • 网站手机页面做多大咖啡seo是什么意思
  • 住房与城乡建设网上办事大厅seo网站怎么搭建
  • 一般网站建设免费二级域名注册网站有哪些
  • 织梦做的网站如何上线南京seo公司
  • 山东省住房城乡建设部网站佣金高的推广平台
  • c php做网站对比电商网站建设平台
  • 高端网站定制建站google首页
  • 网站后台树形菜单样式怎么在百度发布自己的文章
  • 小网站谁有怎么做私人网站
  • 揭阳网站建设托管公司软文推广
  • 永久免费网站申请注册广州新塘网站seo优化
  • 常州网站推广多少钱郑州企业网站优化排名
  • 冷水滩做微信网站百度竞价投放
  • 网站开发最流行的语言免费建站免费网站