© 版权声明:本文为博主原创文章,转载请注明出处

Thymeleaf模板简介

  Thymeleaf模板是一个现代化的服务端java模板引擎对于所有的web和独立环境

  Thymeleaf的主要目标是为你的开发工作流程带来优雅自然的模板 ------ HTML能正确的显示在浏览器中,并且也可以作为静态原型工作,允许开发团队加强协作

  在有Spring框架的模块,与您最喜爱的工具的集成为一体,并能插入自己的功能,Thymeleaf是理想的现代化的HTML5 Web开发JVM ------ 虽然它可以做的更多

  摘自官网:http://www.thymeleaf.org/

  也是SpringBoot推荐使用的模板

SpringBoot配置

1. application.yml配置(application.properties同样)

spring:
thymeleaf:
prefix: classpath:/templates/ # 必须以/结尾,否则报错找不到模板
suffix: .html
mode: HTML5
encoding: UTF-8
cache: false # 关闭缓存,即时刷新。上线后需改为true
servlet:
content-type: text/html

2. 实现Controller

package com.imooc.controller;

import com.imooc.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("th")
public class ThymeleafController { @GetMapping("/index")
public String index(ModelMap map) {
map.addAttribute("name", "thymeleaf-imooc");
return "thymeleaf/index";
} @GetMapping("/center")
public String center() {
return "thymeleaf/center/center";
} @GetMapping("/test")
public String test() {
User user = new User();
return "";
} }

3. 创建Thymeleaf模板

  3.1 index.html(位于 templates/thymeleaf/ 目录下)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
Thymeleaf模板引擎
<h1 th:text="${name}">hello world</h1>
</body>
</html>

  3.2 center.html(位于 templates/thymeleaf/center/ 目录下)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
Thymeleaf模板引擎
<h1>center page</h1>
</body>
</html>

4. 启动SpringBoot项目,测试即可

参考:

https://www.imooc.com/video/16721

SpringBoot使用Thymeleaf模板的更多相关文章

  1. SpringBoot 之Thymeleaf模板.

    一.前言 Thymeleaf 的出现是为了取代 JSP,虽然 JSP 存在了很长时间,并在 Java Web 开发中无处不在,但是它也存在一些缺陷: 1.JSP 最明显的问题在于它看起来像HTML或X ...

  2. springboot整合Thymeleaf模板引擎

    引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</ ...

  3. Springboot整合thymeleaf模板

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

  4. (二)springboot整合thymeleaf模板

    在我们平时的开发中,用了很久的jsp作view显示层,但是标签库和JSP缺乏良好格式的一个副作用就是它很少能够与其产生的HTML类似.所以,在Web浏览器或HTML编辑器中查看未经渲染的JSP模板是非 ...

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

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

  6. SpringBoot系列——Thymeleaf模板

    前言 thymeleaf是springboot官方推荐使用的java模板引擎,在springboot的参考指南里的第28.1.10 Template Engines中介绍并推荐使用thymeleaf, ...

  7. SpringBoot使用thymeleaf模板引擎

    (1).添加pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  8. springboot 引入 thymeleaf 模板

    第一步pom中: <!-- 引入 thymeleaf 模板依赖 --> <dependency> <groupId>org.springframework.boot ...

  9. SpringBoot日记——Thymeleaf模板引擎篇

    开发通常我们都会使用模板引擎,比如:JSP.Velocity.Freemarker.Thymeleaf等等很多,那么模板引擎是干嘛用的? 模板引擎,顾名思义,是一款模板,模板中可以动态的写入一些参数, ...

随机推荐

  1. 转载——为Xamarin更好的开发而改写的库

    本人现今一直奋战在Xamarin.Android,可能有人会疑惑Xamarin本身就是跨平台的,为什么不能直接跨IOS和Android,这个当然是最后的目标,只是现今你连Android都不能拿出符合商 ...

  2. AC日记——[WC2013]糖果公园 cogs 1817

    [WC2013]糖果公园 思路: 带修改树上莫队(模板): 来,上代码: #include <cmath> #include <cstdio> #include <cst ...

  3. Guava源码学习(三)ImmutableCollection

    基于版本:Guava 22.0 Wiki:Immutable collections 0. ImmutableCollection简介 类似于JDK的Collections.unmodifiableX ...

  4. 第一个Mac程序——倒计时v1&v2

    先放效果图: 这是我第一次尝试在Mac平台下编程,之前学过几天IOS开发,一直在等MJ老师更新.闲下来不编程不舒服,给自己,也给老婆编了这个以提醒自己好好学习.v2版加入了各种倒计时.改进了界面.把倒 ...

  5. .Net中的不可变集合(Immutable Collection)简介

    今天发现MS在Nuget上发布了一个Immutable Collection的程序集,提供了对不可变对象的集合的支持. 简单的看了一下,貌似支持的还比较全: ImmutableArray<T&g ...

  6. Android 使用SQLite本地数据库

    参考:http://blog.csdn.net/jianghuiquan/article/details/8569252 在Android平台上,集成了一个嵌入式关系型数据库—SQLite.以SQLi ...

  7. PHP模板解析入门

    学习:李炎恢PHP视频第二季 模板引擎的特点: 1.鼓励分离:让更个系统的可读性和维护性得到提高. 2.促进分工:使得程序员和美工去专心处理自己的设计. 3.比PHP更容易解析:编译文件和缓存文件加载 ...

  8. sql cast函数

    一.语法: CAST (expression AS data_type) 参数说明: expression:任何有效的SQLServer表达式. AS:用于分隔两个参数,在AS之前的是要处理的数据,在 ...

  9. 【机器学习】Mahout算法集

    在Mahout实现的机器学习算法见下表 算法类 算法名 中文名 分类算法 Logistic Regression 逻辑回归 Bayesian 贝叶斯 SVM 支持向量机 Perceptron 感知器算 ...

  10. python学习的推荐教程

    评注: 还未细看, http://www.crifan.com/files/doc/docbook/python_beginner_tutorial/release/html/python_begin ...