Spring boot之使用thymeleaf
操作步骤
(1)在pom.xml中引入thymeleaf;
(2)如何关闭thymeleaf缓存
(3)编写模板文件.html
(4)编写访问模板文件controller
在pom.xml中引入thymeleaf
在pom.xml加入thymeleaf的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
如何关闭thymeleaf缓存
########################################################
###THYMELEAF (ThymeleafAutoConfiguration)
########################################################
#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
编写模板文件.html
编写模板文件src/main/resouces/templates/hello.html:
<!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>Hello World!</title>
</head>
<body>
<h1 th:inline="text">Hello.v.2</h1>
<p th:text="${hello}"></p>
</body>
</html>
编写访问模板文件controller
@Controller
public class TemplateController {
/**
* 返回html模板.
*/
@RequestMapping("/helloHtml")
public String helloHtml(Map<String,Object> map){
map.put("hello","from TemplateController.helloHtml");
return "/helloHtml";
}
}
代码如下:
模板
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<h1>
Hello,thymeleaf
<br />
This is my first thymeleaf demo. <hr /> welcome <span th:text="${name}"></span> </h1>
</body>
</html>
配置文件
########################################################
###THYMELEAF (ThymeleafAutoConfiguration)
########################################################
#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
#\u5f00\u53d1\u8fc7\u7a0b\u5efa\u8bae\u5173\u95ed\u7f13\u5b58.
spring.thymeleaf.cache=false
controller类
package com.kfit; import java.util.Map; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; /**
* 注意:
* 1.在Thymeleaf 模板文件中,标签是需要闭合的,3.0之前是需要闭合的
* 2. thymeleaf 3.0+ 是可以不强制要求闭合的。
*
* 3. 支持同时使用多个模板引擎,比如:thymeleaf和freemarker 可以并存。
* */
@Controller
@RequestMapping("/templates")
public class TemplatesController { /**
* 映射地址是:/templates/hello
* @return
*/
@RequestMapping("/hello")
public String hello(Map<String,Object> map){
//返回的是ModelAndView对象;
// ModelAndView mv = new ModelAndView("hello");
// return mv;
map.put("name","Andy");
return "hello";
} @RequestMapping("/helloFtl")
public String helloFtl(Map<String,Object> map){
map.put("name","Andy");
return "helloFtl";
} }
Spring boot之使用thymeleaf的更多相关文章
- Spring boot 整合 Mybatis + Thymeleaf开发web(二)
上一章我把整个后台的搭建和逻辑给写出来了,也贴的相应的代码,这章节就来看看怎么使用Thymeleaf模板引擎吧,Spring Boot默认推荐Thymeleaf模板,之前是用jsp来作为视图层的渲染, ...
- spring boot 学习(二)spring boot 框架整合 thymeleaf
spring boot 框架整合 thymeleaf spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP.因为若一定要使用 JSP 将无法使用. 注意:本文主要参考学习了大神 ...
- spring boot(四):thymeleaf使用详解
在上篇文章springboot(二):web综合开发中简单介绍了一下thymeleaf,这篇文章将更加全面详细的介绍thymeleaf的使用.thymeleaf 是新一代的模板引擎,在spring4. ...
- Spring Boot(四):Thymeleaf 使用详解
在上篇文章Spring Boot (二):Web 综合开发中简单介绍了一下 Thymeleaf,这篇文章将更加全面详细的介绍 Thymeleaf 的使用.Thymeleaf 是新一代的模板引擎,在 S ...
- (转)Spring Boot(四):Thymeleaf 使用详解
http://www.ityouknow.com/springboot/2016/05/01/spring-boot-thymeleaf.html 在上篇文章Spring Boot (二):Web 综 ...
- 在Spring MVC和Spring Boot中使用thymeleaf模板
Spring MVC: POM: <!-- thymeleaf模板 --> <!-- https://mvnrepository.com/artifact/org.thymeleaf ...
- Spring Boot中使用 Thymeleaf
目录 1.pom.xml引入thymeleaf 2.关闭缓存application.properties 3.编写Controller类 4.模板html 5.运行结果 1.pom.xml引入thym ...
- 关于在spring boot里使用Thymeleaf模板的application.properties配置
spring.thymeleaf.cache=false spring.thymeleaf.encoding=utf- spring.thymeleaf.mode=HTML5 spring.thyme ...
- Spring Boot 整合 Shiro+Thymeleaf
1.导包 <!-- springboot 与 shiro 的集成--> <dependency> <groupId>org.apache.shiro</gro ...
随机推荐
- Java后端技术面试汇总(第三套)
1.基础题 • 怎么解决Hash冲突:(开放地址法.链地址法.再哈希法.建立公共溢出区等)• 写出一个必然会产生死锁的伪代码:• Spring IoC涉及到的设计模式:(工厂模式.单利模式..)• t ...
- mysql设计与优化以及数据库表设计与表开发规范
一.设计问题? 1.主键是用自增还是UUID ? Innodb 中的主键是聚簇索引. 如果主键是自增的,那么每次插入新的记录,记录就会顺序添加到当前索引节点的后续位置,当一页写满,就会自动开辟一个新的 ...
- Python基础教程之dict和set
1. dict Python中的dict等于js中的 map ,使用键-值(key-value)存储,具有极快的查找速度. 如果 我们要根据同学的姓名去查找他的成绩在不用dict的情况下.就需要两个l ...
- 【转载】Linux GCC常用命令
作者:ggjucheng 出处:https://www.cnblogs.com/ggjucheng/archive/2011/12/14/2287738.html 1简介 2简单编译 2.1预处理 2 ...
- JDK1.8中LinkedList的实现原理及源码分析
详见:https://blog.csdn.net/cb_lcl/article/details/81222394 一.概述 LinkedList底层是基于双向链表(双向链表的特点, ...
- linux上安装rz和sz
简介 lrzsz 官网入口:http://freecode.com/projects/lrzsz/ lrzsz是一个unix通信套件提供的X,Y,和ZModem文件传输协议 windows 需要向ce ...
- deep_learning_LSTM长短期记忆神经网络处理Mnist数据集
1.RNN(Recurrent Neural Network)循环神经网络模型 详见RNN循环神经网络:https://www.cnblogs.com/pinard/p/6509630.html 2. ...
- 度限制MST
POJ1639 顶点度数限制的最小生成树 做法:首先把和顶点相连的X条边全部删掉 得到顶点和 X个连通块 然后求出这X个连通块的MST 再把X条边连接回去这样我们就首先求出了X度MST 知道了X度MS ...
- java代码中的坑
1.Integer 类型数值比较: public static void main(String[] args) { // TODO Auto-generated method stu ...
- Acwing-271-杨老师的照相排列(DP)
链接: https://www.acwing.com/problem/content/273/ 题意: 杨老师希望给他的班级拍一张合照. 学生们将站成左端对齐的多排,靠后的排站的人数不能少于靠前的排. ...