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的更多相关文章

  1. SpringBoot 同时整合thymeleaf html、vue html和jsp

    问题描述 SpringBoot如何同时访问html和jsp SpringBoot访问html页面可以,访问jsp页面报错 SpringBoot如何同时整合thymeleaf html.vue html ...

  2. 【Springboot】Springboot整合Thymeleaf模板引擎

    Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...

  3. Spring Boot 2.x 综合示例-整合thymeleaf、mybatis、shiro、logging、cache开发一个文章发布管理系统

    一.概述 经过HelloWorld示例(Spring Boot 2.x 快速入门(上)HelloWorld示例)( Spring Boot 2.x 快速入门(下)HelloWorld示例详解)两篇的学 ...

  4. SpringMVC整合Thymeleaf

    Thymeleaf的介绍 进行JavaWeb开发时主要用到的是JSP,传统的JSP需要在页面中加入大量的JSTL标签,这些标签只能运行在服务器中,前端开发人员维护这些页面比较困难,页面加载速度也比较慢 ...

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

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

  6. Spring Boot 整合 Thymeleaf 完整 Web 案例

    Thymeleaf 是一种模板语言.那模板语言或模板引擎是什么?常见的模板语言都包含以下几个概念:数据(Data).模板(Template).模板引擎(Template Engine)和结果文档(Re ...

  7. Springboot整合thymeleaf模板

    Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...

  8. SpringBoot整合Thymeleaf

    一个整合Thymeleaf与Mybatis的CRUD例子 整合Mybatis例子 一.添加maven依赖 <dependency> <groupId>org.springfra ...

  9. Spring Boot2 系列教程(九)Spring Boot 整合 Thymeleaf

    虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...

随机推荐

  1. Net上传文件

    Net上传文件 最近工作内容涉及到一点前端的内容,把学习到的内容记录下来,在今后的开发过程中,不要犯错.本篇只针对一些刚入职的小白及前端开发人员,大牛请绕道!~ 刚开始我们先不讲上传文件的防范问题,先 ...

  2. uwp,c#,全屏播放保持屏幕响应

    在开发视频app的时候,全屏播放一段时间内没有电脑操作,电脑会自动进入睡眠模式,这时就要多写些代码来保持响应了. (这里使用的是MediaElement播放控件,MediaElement需要手动添加代 ...

  3. json 和对象互相转换

    json 和对象互相转换 导入 Jar 包: import com.fasterxml.jackson.databind.ObjectMapper; Maven 地址: <!-- https:/ ...

  4. if("\v"=="v")来判断IE浏览器

    if(!+"\v1"){ IE代码}else{ 其他浏览器代码} if("\v"=="v"){//true为IE浏览器, document. ...

  5. windows 下安装ElasticSearch方法

    1.https://www.oracle.com/technetwork/java/javase/downloads/jdk12-downloads-5295953.html 在此页面下载安装JDK1 ...

  6. gdb 常用命令总结(精优)

    格式说明: [xxx]:可选参数,即可以指定可以不指定,实际输入的内容是 xxx <xxx>:占位参数,即必须指定的参数,实际输入的内容是 xxx gdb 常用命令: gdb [file] ...

  7. linux运维命令3

    1.grep 逐行搜索所指定的文件或标准输入,并显示匹配模式的每一行. grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C& ...

  8. shell习题第23题:检测网卡流量

    [题目要求] 写一个脚本,检测网卡流量并记录到日志,需要按照如下格式并一分钟统计一次(只需统计外网网卡,网卡名称eth0) 2019-06-07 1:11 eth0 input: 1000bps et ...

  9. electron实现透明点投的方法

    1. electron createWindow 的时候 设置 transparent: true, clickThrough: 'pointer-events' 2. body 上添加 pointe ...

  10. 不遮挡人物弹幕是怎么实现的——图片蒙版效果-webkit-mask

    这是一个实验中的功能,用于设置元素上遮罩层的图像. 一.Values none:默认值,透明的黑色图像层,也就是没有遮罩层. <mask-source>:<mask>或CSS图 ...