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. css3 float排序问题

    css3 float排序问题 有时候发现 会错位 那是因为有个图片大小不一致才会这样 所以要确保每个图片一样

  2. 洛谷 题解 P1225 【黑白棋游戏】

    看见很多dalao写了什么双向BFS,蒟蒻表示不会写啊. 怎么办办? 先来分析一下题目,一眼看去就是一个搜索题,考虑DFS与BFS. 先放一份DFS的代码: #include<bits/stdc ...

  3. java--demo之图书馆管理系统

    aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQEAkACQAAD/4RDQRXhpZgAATU0AKgAAAAgABAE7AAIAAAADTXkAAIdpAAQAA ...

  4. Kafka集群安装及prometheus监控

    前提 zookeeper安装参考:https://www.cnblogs.com/JustinLau/p/11372782.html 其他安装参考:https://www.cnblogs.com/lu ...

  5. vs professional 2019 离线安装包下载方法

    run->cmd D:\vsprofessional2019>vs_professional__1254024763..exe --layout D:\vsprofessional2019 ...

  6. Thrift RPC Golang、C++ Example

    Thrift RPC Example 运行 请直接使用即可,无需拉取任何依赖包. cd $GOPATH/src git clone https://github.com/hunterhug/thrif ...

  7. 十、es6之扩展运算符 三个点(...)

    对象的扩展运算符 对象中的扩展运算符(...)用于取出参数对象中的所有可遍历属性,拷贝到当前对象之中 let bar = { a: 1, b: 2 }; let baz = { ...bar }; / ...

  8. 12种不宜使用的Javascript语法

    Douglas Crockford列出了12种应该避免使用的Javascript语法,我觉得非常值得推广. ============================== 1. == Javascrip ...

  9. S2-032

    前言 S2-032漏洞的影响范围是Struts 2.3.20 - Struts Struts 2.3.28,当开启了动态方法调用时可RCE.这次的漏洞分析以及后面的漏洞分析都是使用的Struts 2. ...

  10. CAS客户端认证流程

    CAS登陆流程 Step 1:浏览器向CAS客户端发起登陆请求,CAS客户端生成“登陆URL”,并把浏览器重定向到该URL 登陆URL: https://${cas-server-host}:${ca ...