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
虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...
随机推荐
- mysql的AB及读写和集群
Mysql的AB及读写 第1章 Mysql的AB配置 1.1 master配置 1.2 slave配置 第2章 读写分离 2.1 安装mycat 2.2 启动mycat 2.3 登录mycat相关问 ...
- iostat (转https://www.cnblogs.com/ftl1012/p/iostat.html)
iostat是I/O statistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视.它的特点是汇报磁盘活动统计情况,同时也会汇报出CPU使用情况.iostat也有一个弱 ...
- WCF-复杂配置
两种模式,一个契约两个实现,两个契约一个实现. 服务类库 宿主 static void Main(string[] args) { ServiceHost sh1 = new ServiceHost( ...
- IBM Security AppScan Standard使用方法
一.常规配置Appscan (安全自动化测试工具) Appscan是web应用程序渗透测试舞台上使用最广泛的工具之一.它是一个桌面应用程序,它有助于专业安全人员进行Web应用程序自动化脆弱性评估.本文 ...
- WUST 设计模式 实验一 单例模式的应用
实验一 单例模式的应用 实验目的 1.掌握单例模式(Singleton)的特点: 2.分析具体问题,使用单例模式进行设计. 实验内容和要求 很多应用项目都有配置文件,这些配置文件里面定义一些应用需要的 ...
- 2019杭电多校二 F. Fantastic Magic Cube (FWT)
大意: 给定$N^3$立方体, 每个单位立方体权值为三个坐标异或, 每次沿坐标轴切一刀, 得分为两半内权值和的乘积, 求切成$n^3$块的最大得分. 可以发现得分与切法无关, 假设每个点权值为$a_i ...
- Recovering BST CodeForces - 1025D (区间dp, gcd)
大意: 给定$n$个数, 任意两个$gcd>1$的数间可以连边, 求是否能构造一棵BST. 数据范围比较大, 刚开始写的$O(n^3\omega(1e9))$竟然T了..优化到$O(n^3)$才 ...
- BZOJ4516 SDOI2016生成魔咒(后缀自动机)
本质不同子串数量等于所有点的len-parent树上父亲的len的和.可以直接维护. #include<iostream> #include<cstdio> #include& ...
- 解决使用绝对定位absolute后,margin:0 auto居中方法失效(转)
https://blog.csdn.net/qq_40678503/article/details/82780680
- (十七)Activitivi5之组任务分配
一.需求分析 我们在实际业务开发过程中,某一个审批任务节点可以分配一个角色(或者叫做组),然后属于这个角色的任何一个用户都可以去完成这个任务节点的审批 二.案例 2.1 方式一:直接流程图配置中写死 ...