Thymeleaf是一个java类库,他是一个xml/xhtml/html5的模板引擎和Struts框架的freemarker模板类似,可以作为mvc的web应用的view层。
Thymeleaf还提供了额外的模块与Spring MVC集成,所以我们可以使用Thymeleaf完全替代jsp。
spring Boot
通过org.springframework.boot.autoconfigure.thymeleaf包对Thymeleaf进行了自动配置。
通过ThymeleafAutoConfiguration类对集成所需要的bean进行自动配置。包括templateResolver,templateEngine,thymeleafViewResolver的配置。

1.pom.xml中添加

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

自动下载thymeleaf的jar包,然后update Project.

2.application.properties中添加thymeleaf配置文件

#Thymeleaf
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

3.之后写一个测试

a.在项目目录下找src/main/resources/templates新建一个dome.html测试网页,注意要在头部引用

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> ,不然找不到网页。
<!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 th:text="${title}"></title>
</head>
<body>
<p th:text="${gr}"></p>
</body>
</html>

b.写测试controller层Thymeleaf.class类

package com.hxzy.myblog.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class Thymeleaf {
@RequestMapping("/hi")
public String helloHtml(Model model){
model.addAttribute("title","Thymeleaf");
model.addAttribute("gr","大家好");
return "dome";
}
}

4.总结:

1.注意html中标签必须严格闭合

2.html中一对标签中使用thymeleaf,用th:text="${title}",title就加在标签内,如

<title th:text="${title}"></title> 。

Thymeleaf 模板 在spring boot 中的引用和应用的更多相关文章

  1. Spring Boot 中配置文件application.properties使用

    一.配置文档配置项的调用(application.properties可放在resources,或者resources下的config文件夹里) package com.my.study.contro ...

  2. Spring Boot中使用Swagger2构建强大的RESTful API文档

    由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...

  3. Spring Boot 中关于自定义异常处理的套路!

    在 Spring Boot 项目中 ,异常统一处理,可以使用 Spring 中 @ControllerAdvice 来统一处理,也可以自己来定义异常处理方案.Spring Boot 中,对异常的处理有 ...

  4. Spring Boot 中的静态资源到底要放在哪里?

    当我们使用 SpringMVC 框架时,静态资源会被拦截,需要添加额外配置,之前老有小伙伴在微信上问松哥Spring Boot 中的静态资源加载问题:"松哥,我的HTML页面好像没有样式?& ...

  5. Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅

    在上一篇Spring中使用JdbcTemplate访问数据库中介绍了一种基本的数据访问方式,结合构建RESTful API和使用Thymeleaf模板引擎渲染Web视图的内容就已经可以完成App服务端 ...

  6. Spring Boot2 系列教程(十一)Spring Boot 中的静态资源配置

    当我们使用 SpringMVC 框架时,静态资源会被拦截,需要添加额外配置,之前老有小伙伴在微信上问松哥 Spring Boot 中的静态资源加载问题:"松哥,我的 HTML 页面好像没有样 ...

  7. Spring Boot2 系列教程(十三)Spring Boot 中的全局异常处理

    在 Spring Boot 项目中 ,异常统一处理,可以使用 Spring 中 @ControllerAdvice 来统一处理,也可以自己来定义异常处理方案.Spring Boot 中,对异常的处理有 ...

  8. Spring Boot中的事务是如何实现的

    本文首发于微信公众号[猿灯塔],转载引用请说明出处 今天呢!灯塔君跟大家讲: Spring Boot中的事务是如何实现的 1. 概述 一直在用SpringBoot中的@Transactional来做事 ...

  9. Dubbo在Spring和Spring Boot中的使用

    一.在Spring中使用Dubbo 1.Maven依赖 <dependency> <groupId>com.alibaba</groupId> <artifa ...

随机推荐

  1. 使用jdk进行数据迁移(sqlite迁移mysql)

    直接粘贴代码 注意:rewriteBatchedStatements=true(加快连接速度) package com.wbg; import org.omg.Messaging.SYNC_WITH_ ...

  2. kernighan lin算法

    这个算法主要用在网络节点的分割.他的思想是将一个网络节点图分割成两个相等的节点集合.为了连接两个社区的边权最小. step1:随机产生两个节点的集合A和B. step2:计算A和B中的每个节点的int ...

  3. Filter,一种aop编程思想的体现

    一.filter简介 filter是Servlet规范里的一个高级特性,只用于对request.response的进行修改. filter提出了FilterChain的概念,客户端请求request在 ...

  4. POJ1286 Necklace of Beads(Polya定理)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9359   Accepted: 3862 Description Beads ...

  5. Maven - 依赖范围<scope></scope>

    6种:

  6. mysql基础,索引

  7. Mac中Mysql开启远程访问(不同于linux直接改配置文件)

    在mac中安装Mysql Workbench 用root用户连上安装的Mysql.  开启远程访问的服务 如下图可以看到是root用户绑定的是localhost  如果不做修改的话,直接访问是访问不了 ...

  8. Linux实战教学笔记15:用户管理初级(下)

    第十四节 用户管理初级(下) 标签(空格分隔): Linux实战教学笔记-陈思齐 ---更多资料点我查看 1,用户查询相关命令id,finger,users,w,who,last,lastlog,gr ...

  9. 8-2 开发接口 (入参是json格式)

    1.开发入参事json格式的接口 import json import tools import flask from .check_session import check_session serv ...

  10. 原生js获取页面中所有checkbox

    var inputs = document.getElementsByTagName("input");//获取所有的input标签对象 var checkboxArray = [ ...