0010Springboot整合thymeleaf
1、pom.xml中添加thymeleaf的起步依赖
2、编写html文件并放在classpath:/templates/路径下
3、编写controller并返回字符串,会到classpath:/templates/路径下找 字符串.html的文件解析并返回
pom.xml中添加thymeleaf的起步依赖
<!--引入thymeleaf模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
若想更改thymeleaf版本号,可在pom.xml中添加如下配置
<properties>
<java.version>1.8</java.version>
<!--更改thymeleaf版本-->
<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
<!-- 布局功能的支持程序 thymeleaf3主程序 适配 layout2以上版本 -->
<!-- thymeleaf2 layout1-->
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
</properties>
编写html文件并放在classpath:/templates/路径下
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>成功</h1>
转义为普通字符,输出字符串"<h1>您好!</h1>"
<div th:text="${hello}"> </div>
<!--不转义为普通字符,即输出为h1标题大小的字符串"您好!"-->
<div th:utext="${hello}"> </div> <!-- th:each每次遍历都会生成当前标签,所以会生成3个h4标签-->
<h4 th:each="user:${users}" th:text="${user}"></h4>
<h4>
<!--/*[[]]相当于th:text*/-->
<span th:each="user:${users}" >[[${user}]]</span>
<!--/*[[]]相当于th:utext*/-->
<span th:each="user:${users}" >[(${user})]</span>
<!--/*加斜杠星形式的代码在解析时会被移除,上边带th:utext形式的代码,如果注释中不加斜杠星,
访问该页面解析时会报错,审查元素时看不到这部分注释,其他形式的注释在审查元素时可以看到*/-->
</h4>
</body>
</html>
编写controller并返回字符串,会到classpath:/templates/路径下找 字符串.html的文件解析并返回
package com.myself.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import java.util.Arrays;
import java.util.Map; @Controller
public class HelloController { @RequestMapping("/hello")
//注意此处不能写@ResponseBody,否则就不会解析success.html文件了,而是之间返回"success"字符串给页面
//同理,类上也不能写@RestController,因为它相当于@Controller和@ResponseBody的合体
//@ResponseBody
public String hello(Map<String,Object> map){
map.put("hello","<h1>您好!</h1>");
map.put("users", Arrays.asList("张三","李四","王五"));
return "success";
}
} 另外thymeleaf的语法可访问thymeleaf的官网进行学习,为方便查看也可导出pdf进行查看

若有理解不到之处,望指正!
0010Springboot整合thymeleaf的更多相关文章
- SpringBoot 同时整合thymeleaf html、vue html和jsp
问题描述 SpringBoot如何同时访问html和jsp SpringBoot访问html页面可以,访问jsp页面报错 SpringBoot如何同时整合thymeleaf html.vue html ...
- 【Springboot】Springboot整合Thymeleaf模板引擎
Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...
- Spring Boot 2.x 综合示例-整合thymeleaf、mybatis、shiro、logging、cache开发一个文章发布管理系统
一.概述 经过HelloWorld示例(Spring Boot 2.x 快速入门(上)HelloWorld示例)( Spring Boot 2.x 快速入门(下)HelloWorld示例详解)两篇的学 ...
- SpringMVC整合Thymeleaf
Thymeleaf的介绍 进行JavaWeb开发时主要用到的是JSP,传统的JSP需要在页面中加入大量的JSTL标签,这些标签只能运行在服务器中,前端开发人员维护这些页面比较困难,页面加载速度也比较慢 ...
- spring boot 学习(二)spring boot 框架整合 thymeleaf
spring boot 框架整合 thymeleaf spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP.因为若一定要使用 JSP 将无法使用. 注意:本文主要参考学习了大神 ...
- Spring Boot 整合 Thymeleaf 完整 Web 案例
Thymeleaf 是一种模板语言.那模板语言或模板引擎是什么?常见的模板语言都包含以下几个概念:数据(Data).模板(Template).模板引擎(Template Engine)和结果文档(Re ...
- Springboot整合thymeleaf模板
Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...
- SpringBoot整合Thymeleaf
一个整合Thymeleaf与Mybatis的CRUD例子 整合Mybatis例子 一.添加maven依赖 <dependency> <groupId>org.springfra ...
- Spring Boot2 系列教程(九)Spring Boot 整合 Thymeleaf
虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...
随机推荐
- Asp.net SignalR 实现服务端消息实时推送到所有Web端
ASP .NET SignalR是一个ASP .NET 下的类库,可以在ASP .NET 的Web项目中实现实时通信.实际上 Asp.net SignalR 2 实现 服务端消息推送到Web端, 更加 ...
- 【转】git2.9.2使用总结
git2.9.2使用总结 1.系统:Windows7 2.git版本:2.9.2 由于我的git版本是最新版,后面我出现的坑就是最新版本的问题. 3.托管环境:开源中国的码云 . 使用步骤 1.在码云 ...
- python 字体染色
字体染色 <font face="黑体">我是黑体字</font> <font face="微软雅黑">我是微软雅黑< ...
- Java基础---Java常量
常量:在程序运行期间不变的量 分类: 类型 含义 数据举例 整数常量 所有整数 0,1, 567, -9 小数常量 所有小数 0.0, -0.1, 2.55 字符常量 单引号引起来,只能写一个字符, ...
- Essential C++ Reading Notes
Chapter1 P6, 1.2 Why //#include<string> we still can use "string user_name"? -->c ...
- C++Primer 5th Chap6 Functions
局部静态变量,关键字static修饰,即使函数结束执行也不受影响,生存期直到程序终止. java中static的单一存储空间的概念与其或有异曲同工之妙. 函数的形参可以无名,但有名可以使其意义更加清晰 ...
- 利用Python进行数据分析 第4章 IPython的安装与使用简述
本篇开始,结合前面所学的Python基础,开始进行实战学习.学习书目为<利用Python进行数据分析>韦斯-麦金尼 著. 之前跳过本书的前述基础部分(因为跟之前所学的<Python基 ...
- Python之数字的四舍五入(round(value, ndigits) 函数)
round(value, ndigits) 函数 print(round(1.23)) # 1 print(round(1.27)) # 1 print(round(1.23,1)) # 1.2 第二 ...
- 1254: 盒子游戏(Java)
WUSTOJ 1254: 盒子游戏 参考博客 叶剑飞Victor的博客 盒子游戏--爱程序网 原理是从上面博客看的,我另外补充了几幅图,方便理解 Description 有两个相同的盒子,其中一个装了 ...
- go 结构的方法总结
方法可以与命名类型或命名类型的指针关联. 刚刚看到的两个 Abs 方法.一个是在 *Vertex 指针类型上,而另一个在 MyFloat 值类型上. 有两个原因需要使用指针接收者.首先避免在每个方法调 ...