Thymeleaf 模板 在spring boot 中的引用和应用
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 中的引用和应用的更多相关文章
- Spring Boot 中配置文件application.properties使用
一.配置文档配置项的调用(application.properties可放在resources,或者resources下的config文件夹里) package com.my.study.contro ...
- Spring Boot中使用Swagger2构建强大的RESTful API文档
由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...
- Spring Boot 中关于自定义异常处理的套路!
在 Spring Boot 项目中 ,异常统一处理,可以使用 Spring 中 @ControllerAdvice 来统一处理,也可以自己来定义异常处理方案.Spring Boot 中,对异常的处理有 ...
- Spring Boot 中的静态资源到底要放在哪里?
当我们使用 SpringMVC 框架时,静态资源会被拦截,需要添加额外配置,之前老有小伙伴在微信上问松哥Spring Boot 中的静态资源加载问题:"松哥,我的HTML页面好像没有样式?& ...
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
在上一篇Spring中使用JdbcTemplate访问数据库中介绍了一种基本的数据访问方式,结合构建RESTful API和使用Thymeleaf模板引擎渲染Web视图的内容就已经可以完成App服务端 ...
- Spring Boot2 系列教程(十一)Spring Boot 中的静态资源配置
当我们使用 SpringMVC 框架时,静态资源会被拦截,需要添加额外配置,之前老有小伙伴在微信上问松哥 Spring Boot 中的静态资源加载问题:"松哥,我的 HTML 页面好像没有样 ...
- Spring Boot2 系列教程(十三)Spring Boot 中的全局异常处理
在 Spring Boot 项目中 ,异常统一处理,可以使用 Spring 中 @ControllerAdvice 来统一处理,也可以自己来定义异常处理方案.Spring Boot 中,对异常的处理有 ...
- Spring Boot中的事务是如何实现的
本文首发于微信公众号[猿灯塔],转载引用请说明出处 今天呢!灯塔君跟大家讲: Spring Boot中的事务是如何实现的 1. 概述 一直在用SpringBoot中的@Transactional来做事 ...
- Dubbo在Spring和Spring Boot中的使用
一.在Spring中使用Dubbo 1.Maven依赖 <dependency> <groupId>com.alibaba</groupId> <artifa ...
随机推荐
- word中磅和mm的换算
字号 磅 毫米数 初号 42 14.82 小初 36 12.70 一号 26 9.17 小一 24 8.47 二号 22 7.76 小二 18 6.35 三号 16 5.64 小三 15 5.29 四 ...
- Shell编程学习之重定向
这一篇讲一下重定向 有些时候你想要保存某些命令产生的输出而不是在显示器上显示它. 为了应对这样的问题 bash shell 也就提供了一些重定向的操作符. 我们先了解一些基本的应用. 输出重定向 输出 ...
- C# return语句
一.C# return语句 return语句用于终止它出现在其中的方法的执行,并将控制返回给调用方法. 语法格式如下: return ...;return语句还可以返回一个可选值.如果方法为void类 ...
- 【软件笔记】 ◆笔记·I◆ 各类冷门函数细解
[软件笔记·I] 各类冷门函数细解 ■题外话■ 总觉得作为一个志向远大的 coder (٩(◕‿◕。)۶),我觉得单单只会做题是不够的所以我开始尝试自己编写软件!初入道的我并不知道C++其实并不太适合 ...
- js jquery 权限单选 bug修改以及正确代码 购物车数量加减
效果图废话不多直接上代码 用的avalon渲染,其实都是一样的 <div class="shop-arithmetic"> <a href="javas ...
- dts--framework(一)
dts 大体框架 framework 定义类 定义方法 tests framework调用所需要的函数 ./dpdk/usertools/cpu_layout.py /sys/devices/syst ...
- WRITE命令 書式設定オプション
書式設定オプション WRITE 命令では.さまざまな書式設定オプションが使用することができます. 構文 WRITE ....f option. 全データ型の書式設定オプション オプション 機能 LEF ...
- Sqoop 工具使用
Sqoop 是什么及安装 Hadoop sqoop Apache sqoop (SQL to Hadoop) Sqoop is a tool designed to transfer data bet ...
- Windows GitLab使用全过程
1.首先安装Git 1.1.下载网站: https://git-for-windows.github.io/ 1.2.安装Git参考网站 http://blog.csdn.net/u012614287 ...
- PHP.32-TP框架商城应用实例-后台8-商品相册-添加
商品相册[是商品的其他相片] 添加相册需求: 每张图片生成三张缩略图{50*50.350*350.650*650} 1.建表p39_goods_pic{id,pic,sm_pic,mid_pic,bi ...