操作步骤

(1)在pom.xml中引入thymeleaf;

(2)如何关闭thymeleaf缓存

(3)编写模板文件.html

(4)编写访问模板文件controller

在pom.xml中引入thymeleaf

在pom.xml加入thymeleaf的依赖:

<dependency>

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

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

</dependency>

如何关闭thymeleaf缓存

########################################################
###THYMELEAF (ThymeleafAutoConfiguration)
########################################################
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
# ;charset=<encoding> is added
#spring.thymeleaf.content-type=text/html
# set to false for hot refresh
spring.thymeleaf.cache=false

编写模板文件.html

编写模板文件src/main/resouces/templates/hello.html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1 th:inline="text">Hello.v.2</h1>
<p th:text="${hello}"></p>
</body>
</html>

编写访问模板文件controller

@Controller
public class TemplateController {

/**
* 返回html模板.
*/
@RequestMapping("/helloHtml")
public String helloHtml(Map<String,Object> map){
map.put("hello","from TemplateController.helloHtml");
return "/helloHtml";
}

}

代码如下:

模板

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<h1>
Hello,thymeleaf
<br />
This is my first thymeleaf demo. <hr /> welcome <span th:text="${name}"></span> </h1>
</body>
</html>

配置文件

########################################################
###THYMELEAF (ThymeleafAutoConfiguration)
########################################################
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
# ;charset=<encoding> is added
#spring.thymeleaf.content-type=text/html
# set to false for hot refresh
#\u5f00\u53d1\u8fc7\u7a0b\u5efa\u8bae\u5173\u95ed\u7f13\u5b58.
spring.thymeleaf.cache=false

controller类

package com.kfit;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; /**
* 注意:
* 1.在Thymeleaf 模板文件中,标签是需要闭合的,3.0之前是需要闭合的
* 2. thymeleaf 3.0+ 是可以不强制要求闭合的。
*
* 3. 支持同时使用多个模板引擎,比如:thymeleaf和freemarker 可以并存。
* */
@Controller
@RequestMapping("/templates")
public class TemplatesController { /**
* 映射地址是:/templates/hello
* @return
*/
@RequestMapping("/hello")
public String hello(Map<String,Object> map){
//返回的是ModelAndView对象;
// ModelAndView mv = new ModelAndView("hello");
// return mv;
map.put("name","Andy");
return "hello";
} @RequestMapping("/helloFtl")
public String helloFtl(Map<String,Object> map){
map.put("name","Andy");
return "helloFtl";
} }

Spring boot之使用thymeleaf的更多相关文章

  1. Spring boot 整合 Mybatis + Thymeleaf开发web(二)

    上一章我把整个后台的搭建和逻辑给写出来了,也贴的相应的代码,这章节就来看看怎么使用Thymeleaf模板引擎吧,Spring Boot默认推荐Thymeleaf模板,之前是用jsp来作为视图层的渲染, ...

  2. spring boot 学习(二)spring boot 框架整合 thymeleaf

    spring boot 框架整合 thymeleaf spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP.因为若一定要使用 JSP 将无法使用. 注意:本文主要参考学习了大神 ...

  3. spring boot(四):thymeleaf使用详解

    在上篇文章springboot(二):web综合开发中简单介绍了一下thymeleaf,这篇文章将更加全面详细的介绍thymeleaf的使用.thymeleaf 是新一代的模板引擎,在spring4. ...

  4. Spring Boot(四):Thymeleaf 使用详解

    在上篇文章Spring Boot (二):Web 综合开发中简单介绍了一下 Thymeleaf,这篇文章将更加全面详细的介绍 Thymeleaf 的使用.Thymeleaf 是新一代的模板引擎,在 S ...

  5. (转)Spring Boot(四):Thymeleaf 使用详解

    http://www.ityouknow.com/springboot/2016/05/01/spring-boot-thymeleaf.html 在上篇文章Spring Boot (二):Web 综 ...

  6. 在Spring MVC和Spring Boot中使用thymeleaf模板

    Spring MVC: POM: <!-- thymeleaf模板 --> <!-- https://mvnrepository.com/artifact/org.thymeleaf ...

  7. Spring Boot中使用 Thymeleaf

    目录 1.pom.xml引入thymeleaf 2.关闭缓存application.properties 3.编写Controller类 4.模板html 5.运行结果 1.pom.xml引入thym ...

  8. 关于在spring boot里使用Thymeleaf模板的application.properties配置

    spring.thymeleaf.cache=false spring.thymeleaf.encoding=utf- spring.thymeleaf.mode=HTML5 spring.thyme ...

  9. Spring Boot 整合 Shiro+Thymeleaf

    1.导包 <!-- springboot 与 shiro 的集成--> <dependency> <groupId>org.apache.shiro</gro ...

随机推荐

  1. JS中的继承(下)

    JS中的继承(下) 在上一篇 JS中的继承(上) 我们介绍了3种比较常用的js继承方法,如果你没看过,那么建议你先看一下,因为接下来要写的内容, 是建立在此基础上的.另外本文作为我个人的读书笔记,才疏 ...

  2. Python验证码登录(Tesseract安装配置)

    1.安装py库:pytesseract,PIL pip install pytesseract pip install PILLOW 如果安装时,出现权限不足: pip install --user ...

  3. redis 学习(10)-- redis 慢查询

    redis 慢查询 什么是慢查询 MySQL会记录下查询超过指定时间的语句,我们将超过指定时间的SQL语句查询称为慢查询,都记在慢查询日志里. redis 的生命周期 客户端向Redis服务器发送命令 ...

  4. http的导图

  5. spring boot jpa criteria api是如何生成JPQL的

    当我们使用entityManager.createQuery(query)时,我们发现entityManager的注入对象如下: 也就是它:org.springframework.orm.jpa.Lo ...

  6. [转载]C++STL概述

    来源:https://www.cnblogs.com/dyllove98/p/3214898.html 什么是容器 首先,我们必须理解一下什么是容器,在 C++ 中容器被定义为:在数据存储上,有一种对 ...

  7. 正确的安装和使用nvm(mac)<转>

    前言 目前主流的node版本管理工具有两种,nvm和n.两者差异挺大的,具体分析可以参考一下淘宝FED团队的一篇文章: 管理 node 版本,选择 nvm 还是 n? 总的来说,nvm有点类似于 Py ...

  8. 24、Nginx缓存web服务

    通常情况下缓存是用来减少后端压力, 将压力尽可能的往前推, 减少后端压力,提高网站并发延时 1.缓存常见类型 服务端缓存 代理缓存, 获取服务端内容进行缓存 客户端浏览器缓存 Nginx代理缓存原理 ...

  9. Backtracking(一)

    LeetCode中涉及到回溯的题目有通用的解题套路: 46. permutations 这一类回溯题目中的基础中的基础,无重复数字枚举: /* Given a collection of distin ...

  10. 紫书 例题 10-11 UVa 11181(概率计算)

    这道题不能凭感觉做了.要套公式 r个人买了东西叫事件E, 第i个人买东西的概率叫做事件Ei 求得是P(E|Ei), 则P(E|Ei)= P(E|Ei)/ P(E) 那么P(E)可以枚举求得, 用递归求 ...