首页 > 其他分享 >Thymeleaf

Thymeleaf

时间:2023-07-24 21:45:40浏览次数:25  
标签:对象 thymeleaf Thymeleaf html th 页面

springboot 集成 Thymeleaf

示例:D:\java\demo\student\thymeleaf

1. Thymeleaf 介绍

2. 依赖导入

在 Spring Boot 中使用 thymeleaf 模板需要引入依赖,可以在创建项目工程时勾选 Thymeleaf,也可以创建之后再手动导入。

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

如果在 html 文件中使用 thymeleaf 模板,则需要在标签中引入 th 命名空间

<html xmlns:th="http://www.thymeleaf.org">

3. Thymeleaf 配置

Thymeleaf 默认是开启页面缓存的,所以在开发的时候,需要关闭这个页面缓存的时候在 application.yml 配置文件中关闭:

spring:
    thymeleaf:
        cache: false #关闭缓存

如果不关闭缓存,就会导致页面没法及时看到更新后的效果。
比如修改了一个文件,tomcat 已经重新部署了,但刷新页面还是之前的页面,就是因为缓存引起的。

4. Thymeleaf 的使用

4.1 访问静态(错误)页面

制作一个 404 页面和 500 页面,以便出错时给用户一个友好的展示,而不至于一堆异常信息抛出来。
Spring Boot 会自动识别模板目录 templates/ 下的 404.html 和 500.html 文件。
resources/templates/404.html
在 templates/目录下新建一个 error 文件夹,专门放置错误的 html 页面,然后分别打印些信息。

4.2 Controller类的配置

在 Controller 层我们不能使用 @RestController 注解,而应该使用 @Controller 注解。
@RestController 注解,自动会把返回的数据转成 json 格式。但我们在使用 thymeleaf 模板时,返回的是视图文件名。

@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {
    @RequestMapping("/test500")
    public String test500() {
        int i = 1 / 0;
        return "index";
    }
}

比如上面的 Controller 中是返回到 index.html 页面,如果使用@RestController 的话,会把 index 当作 String 解析了,直接返回"index"字符串到页面,而不是去找 index.html 页面,所以在使用模板时要用@Controller 注解。

4.3 Thymeleaf 中处理数据

4.3.1 对象

thymeleaf 模板中应该如何处理对象信息呢?假如在做个人博客的时候,需要给前端传博主相关信息来展示,分为三步:

  • 1封装成对象
public class Bloger{
    private Long id;
    private String name;
    private String pass;
}
  • 2在 controller 层中初始化:
    先初始化一个 Blogger 对象,然后将该对象放到 Model 中,然后返回到 blogger.html 页面去渲染。
@GetMapping("/getBlogger")
public String getBlogger(Model model){
    Blogger blogger = new Blogger(1L, "lijunrui","123456");
    model.addAttribute("blogger", blogger);
    return "blogger";
}
  • 3.在 blogger.html 页面中渲染:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>博主信息</title>
    </head>
<body>
<form action="" th:object="${blogger}">
    用户编号:<input name="id" th:value="${blogger.id}"/><br>
    用户姓名:<input type="text" name="username" th:value="${blogger.getName()}" /><br>
    登陆密码:<input type="text" name="password" th:value="*{pass}" />
</form>
</body>
</html>

在 thymeleaf 模板中,使用 th:object="${}"来获取对象信息.
在 from 表单中可以有三种方式来获取对象属性:

  • 1、使用 th:value="*{属性名}"
  • 2、使用 th:value="${对象.属性名}",对象指的是上面使用 th:object 获取的对象
  • 3、使用 th:value="${对象.get 方法}",对象指的是上面使用 th:object 获取的对象

标签:对象,thymeleaf,Thymeleaf,html,th,页面
From: https://www.cnblogs.com/li-len/p/17578424.html

相关文章

  • Spring+SpringMVC+Thymeleaf 示例
    目录参考资料开发工具1环境准备2使用maven模板构建war工程2.1File->New->Other2.2选择【MavenProject】2.3选择maven模板2.4输入GroupId、ArtifactId、package2.5生成的项目工程2.6配置项目2.7完整工程目录3配置SpringMVC3.1引入Springjar3.2配置......
  • 68. 使用thymeleaf报异常:Not Found, status=404
    【从零开始学习SpirngBoot—常见异常汇总】      我们按照正常的流程编码好了controller访问访问方法/hello,对应的是/templates/hello.html文件,但是在页面中还是抛出了错误信息:WhitelabelErrorPageThisapplicationhasnoexplicitmappingfor/error,soyouaresee......
  • 第四天(Thymeleaf,MVC自动配置原理,,配置项目环境及首页,页面国际化,登录+拦截器)
    ThymeleafMVC自动配置原理ContentNegotiatingViewResolver内容协商视图解析器转换器和格式化器配置项目环境及首页页面国际化中英切换登录+拦截器......
  • Thymeleaf对象
    1. Thymeleaf 基本对象   115-117模板引擎提供了一组内置的对象,这些内置的对象可以直接在模板中使用,这些对象由#号开始引用,我们比较常用的内置对象1.2 #request 表示 HttpServletRequest1.3  #session 表示 HttpSession 对象1.4 session 对象,表示 HttpSession 对......
  • 基于SpringBoot+MyBatis+Thymeleaf的学生管理系统搭建
    学生管理系统Maven工程搭建【步骤】:打开IDEA工具,选择创建一个新工程。选择SpringInitializr,点击Next按钮。大家也可以通过Spring提供的在线创建的方式创建工程,访问(https://start.spring.io),然后将创建后的工程代码zip包解压后,使用IDEA导入工程。这种方式不在本文描述......
  • thymeleaf for循环第一次后中断循环
     thymeleaffor循环第一次后中断循环<divth:each="processList,iterStat:${dict.processList}"><th:blockth:if="${iterStat.index==0}"><spanstyle="width:80%;display:block;"class="p......
  • Thymeleaf
    Thymeleaf视图模板技术JavaWeb教程目录|代码重工(gitee.io)6.thymeleaf的部分标签1)使用步骤:添加jar,新建ViewBaseServlet(有两个方法),配置两个<context-param>:view-prefix,view-suffix2)部分标签:<th:if>,<th:unless>,<th:each>,<th:text>7、Thym......
  • SpringBoot中使用Thymeleaf常用功能(一):表达式访问数据
    环境搭建:  创建一个Maven项目,按照Maven项目的规范,在src/main/下新建一个名为resources的文件夹,并在下面新建static和templates文件夹。 ① 修改pom.xml:<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi......
  • springboot 分析源码欢迎页和图标-> thymeleaf模板引擎常用语法->扩展
    欢迎页: icon: 注意点: thymeleaf模板引擎1.使用thymeleaf模板引擎前要导入对应依赖包2.阅读源码:根据源码说明我们可以将html文件放置在templates目录下,然后通过controller进行跳转即可 controller类://在templates下的东西需要通过controller类来跳转,//需要导入......
  • thymeleaf学习问题整理
    使用配置<properties><java.version>1.8</java.version><thymeleaf.version>3.0.9.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version></p......