前言

Springboot 推荐使用Thymeleaf做视图层。Thymeleaf支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。相比JSP,Thymeleaf使用方便、简单。可以与 SpringMVC 完美集成。

Springboot 集成Thymeleaf

创建一个Springboot项目,添加以下依赖

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

templates文件夹下新建文件index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>你好</h1>
<h1 th:text="${text}">1</h1>
</body>
</html>

新建控制类

@Controller
public class testController { @RequestMapping("/hello")
public String hello(@RequestParam(defaultValue = "jotal",required = false)String text, Model model) {
model.addAttribute("text", text);
return "index";
}
}

在application.yml中完成Thymeleaf的相关配置

spring:
thymeleaf:
# 前缀
prefix: classpath:/templates/
# 后缀
suffix: .html
mode: html
# 关闭缓存 ,浏览器刷新就显示修改的视图,开发阶段一般关闭
cache: false

测试

springboot笔记06——使用Thymeleaf模板引擎的更多相关文章

  1. SpringBoot入门篇--使用Thymeleaf模板引擎进行页面的渲染

    在做WEB开发的时候,我们不可避免的就是在前端页面之间进行跳转,中间进行数据的查询等等操作.我们在使用SpringBoot之前包括我在内其实大部分都是用的是JSP页面,可以说使用的已经很熟悉.但是我们 ...

  2. JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎

    上篇博客我们聊了<JavaEE开发之SpringBoot工程的创建.运行与配置>,从上篇博客的内容我们不难看出SpringBoot的便捷.本篇博客我们继续在上篇博客的基础上来看一下Spri ...

  3. SpringBoot项目的前端+thymeleaf模板引擎

    SpringBoot项目创建之后,后台的框架是SpringMVC.但前端的resource和template目录都是空的.这个时候需要创建前台页面. 习惯上,我们会创建JSP,但是,SpringBoo ...

  4. (二)SpringBoot基础篇- 静态资源的访问及Thymeleaf模板引擎的使用

    一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf ...

  5. (二)SpringBoot2.0基础篇- 静态资源的访问及Thymeleaf模板引擎的使用

    一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf ...

  6. 【Springboot】Springboot整合Thymeleaf模板引擎

    Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...

  7. SpringBoot使用thymeleaf模板引擎

    (1).添加pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  8. SpringBoot:2.SpringBoot整合Thymeleaf模板引擎渲染web视图

    在Web开发过程中,Spring Boot可以通过@RestController来返回json数据,那如何渲染Web页面?Spring Boot提供了多种默认渲染html的模板引擎,主要有以下几种: ...

  9. 九、SpringBoot集成Thymeleaf模板引擎

    Thymeleaf咋读!??? 呵呵,是不是一脸懵逼...哥用我的大学四级英文知识告诉你吧:[θaimlif]. 啥玩意?不会音标?...那你就这样叫它吧:“赛母李府”,大部分中国人是听不出破绽的.. ...

随机推荐

  1. 作业——12 hadoop大作业

    作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3339 Hadoop综合大作业 1.以下是爬虫大作业产生的csv文件 ...

  2. [BUAA软工]团队任务拆解

    团队任务分解 本次博客为项目alpha的任务拆解 WBS 任务估计 任务 时长(h) 语音模块:监听部分 5h 语音模块:消息处理:api 4h 语音模块:消息处理:内部数据结构 3h 语音模块:消息 ...

  3. RecyclerView 实现快速滚动

    RecyclerView 实现快速滚动 https://www.cnblogs.com/mamamia/p/8311449.html

  4. 网络公开课和MOOC资源

    美国(USA) 1. 麻省理工学院开放课程(Free Online Course Materials/ MIT OpenCourseWare) 2. 耶鲁大学开放课程(Online Video Lec ...

  5. el-cascader遇到一个坑的问题

    经仔细分析,如果二级和三级的value一样,就会出现这个问题.

  6. Docker打包部署前端项目与负载均衡

    设置淘宝镜像 npm install -g cnpm --registry=https://registry.npm.taobao.org //在home/fn1 home/fn2放入项目和nginx ...

  7. elementui---日期格式的选择

    在用elementui做数据提交的时候,默认的时间格式一个对象,好麻烦,主要对时间进行格式限制,具体方法如下: <el-form-item :label="$t('oneCard.bi ...

  8. Flutter中通过普通的点击事件修改TextFormField的值

    import 'package:flutter/material.dart'; import 'package:zhongfa_apps/widget/public/PublicWidget.dart ...

  9. Jmeter获取 json字符的另外一种写法

    在jmeter使用过程中,我们经常会看到接口返回数据类型为application/json,也就时我们常说的json格式. 而在功能测试时,我们经常会要对它的结果进行断言,确认结果是否与预期一致,有时 ...

  10. js 类型判断