前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot(14)-thymeleaf

springboot(14)-thymeleaf

作者头像
叔牙
发布2020-11-19 15:17:12
1.4K0
发布2020-11-19 15:17:12
举报
文章被收录于专栏:一个执拗的后端搬砖工

springboot&thymeleaf

在本文中,我们将讨论如何为Spring Boot应用程序设置和使用Thymeleaf。

介绍

一般来说,Jsp被称为生成Spring MVC应用程序时生成HTML的默认选择。JSP是一种成熟的技术并提供了许多好处,然而,有一些点我们需要注意。

  • JSP不是模板引擎。这些文件在作为Web内容之前被编译到servlet。
  • Thymeleaf是一个真正的模板引擎,它采用HTML文件,解析它,然后生成正在服务的Web内容。
  • 当与JSP视图比较时,Thymeleaf更像是一个HTML类别的视图。
  • 它允许使用模板作为原型,意味着它们可以被视为静态文件。

springboot提供了自动配置来支持Thymeleaf。

springboot集成Thymeleaf

springboot集成Thymeleaf分几个步骤,我们逐步分析一下。

引入maven依赖

springboot将为Thymeleaf提供自动配置。在pom.xml中添加spring-boot-starter-thymeleaf依赖项来启用此自动配置。

<!-- thymeleaf -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

不需要其他的配置,springboot将注入所有必需的配置来使用Thymeleaf。

Thymeleaf模板

我们可以将HTML模板放置在src/main/resources/templates目录下。springboot将自动选择模板。让我们创建一个基于Thymeleaf的示例HTML模板(index.html):

<!DOCTYPE html>

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

<head>

<meta charset="UTF-8">

<title>Title</title>

</head>

<body>

<h2>用户信息</h2>

<div>

<p>ID:<span th:text="${user.id}"></span></p>

<p>名字:<span th:text="${user.name}"></span></p>

<p>年龄:<span th:text="${user.age}"></span></p>

</div>

</body>

</html>

我们仔细看一下index.html模板。

  • 第一行是标准H5声明标签。
  • 第二行thymeleaf的命名空间。
  • <meta >标签定义字符编码。

在我们的示例中,将打印用户信息,并获取th:text表达式的值来渲染{user.id},{user.name}和

自定义模板目录

默认情况下,springboot自动去src/resources/templates目录下获取html模板,但是我们也可以使用自定义配置

来改变默认目录。

在application.properties文件中设置spring.thymeleaf.template-resolver-order=0。然后下一步创建自定义

ClassLoaderTemplateResolver。

@Configuration

public class CustomConfig implements WebMvcConfigurer {

@Bean

public ClassLoaderTemplateResolver customTemplateResolver() {

ClassLoaderTemplateResolver configurer = new ClassLoaderTemplateResolver();

configurer.setPrefix("customLocation/");

configurer.setSuffix(".html");

configurer.setTemplateMode(TemplateMode.HTML);

configurer.setCharacterEncoding("UTF-8");

configurer.setOrder(0); // this is important. This way spring //boot will listen to both places 0 and 1

configurer.setCheckExistence(true return configurer;

return configurer;

}

}

请求控制器

在这个步骤中,我们将创建一个Spring MVC控制器,我们的控制器将执行以下内容。

  • 处理获取/user/{id}路径映射的GET请求。
  • 返回名称为“index”的视图。Spring Boot视图解析器将从以下位置src/main/resources/templates/index加载HTML模板。

启动应用&测试

启动应用:

@SpringBootApplication

public class App

{

public static void main(String[] args) {

SpringApplication.run(App.class, args);

}

}

浏览器输入http://localhost:8080/user/1:

总结

在本文中,我们介绍了如何为springboot应用程序设置和使用Thymeleaf。我们介绍了不同的springboot Thymeleaf配置以及如何定制Thymeleaf行为。希望能够带来帮助。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-10-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 PersistentCoder 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档