操作步骤

(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. Linux 测试IP和端口是否能访问

    一. 使用wget判断 wget是linux下的下载工具,需要先安装. 用法: wget ip:port 连接存在的端口 转自:https://blog.csdn.net/weixin_3768923 ...

  2. maven引用本地jar,并打包部署

    由于项目需要的一个jar在maven仓库里没有,又不想把jar手动导入仓库.然而百度的几个处理方式都是一样的方法,我却没有一个成功的.于是就请教了大神,大神也是各种测试,终于成功实现了,实现如下: 新 ...

  3. LintCode 29---交叉字符串

    public class Solution { /** * @param s1: A string * @param s2: A string * @param s3: A string * @ret ...

  4. MySQL--高性能MySQL笔记一

    链接管理与安全性: 每个客户端连接都在服务器进程中拥有一个线程. MySQL5.5以及更新的版本提供了一个API,支持线程池插件,可以使用池中少量的线程服务大量的链接. 认证基于用户名.密码和原始主机 ...

  5. 使用 java 创建你的第一个区块链(第一部分)

    本系列教程的目的是帮助您了解如何开发区块链技术. 在本教程中,我们将: 创建你的第一个(非常)基本的“区块链”. 实施简单的工作证明(采矿)系统. 惊叹于可能性. (我假设您对面向对象编程有基本的了解 ...

  6. cassandra查询效率探讨

    cassandra目前提倡的建表与查询方式为CQL方式,传统的cassandra-cli相关的api由于性能问题将逐步淘汰,而cassandra-cli也将在2.2版本之后被淘汰. 在CQL中,可以利 ...

  7. MySQL select之后再update

    1.先查询页面 而后再根据查询的结果来更改数据库,可以使用SELECT …… FOR UPDATE 来实现,具体的代码如下 SELECT * FROM chat //查询的表 //查询的条件 FOR ...

  8. CentOS6.5增加挂载点容量

    一.背景:因为公司虚拟机 (/) 目录容量过小,导致一些任务不能正常执行,需要给虚拟机扩容 二.操作: 初始磁盘情况: 1.使用 df 命令查看磁盘与目录的容量: [root@shaonian ~]# ...

  9. nginx服务学习第一章

    一.ubuntu系统安装nginx服务 # apt-get install nginx 二.nginx.config配置文件详解 配置文件结构: 全局块(全局变量) events{ } http{ h ...

  10. jar包混淆和防反编译工具proguard使用简介

    平时都是用java语言做开发,特殊情况下,需要对编译出的jar包混淆,防止被轻易的反编译出来看到源码,用的proguard工具,下面简单记录一下工具使用过程. 1.下载程序包,可以去https://w ...