SpringBoot使用Thymeleaf模板
© 版权声明:本文为博主原创文章,转载请注明出处
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模板的更多相关文章
- SpringBoot 之Thymeleaf模板.
一.前言 Thymeleaf 的出现是为了取代 JSP,虽然 JSP 存在了很长时间,并在 Java Web 开发中无处不在,但是它也存在一些缺陷: 1.JSP 最明显的问题在于它看起来像HTML或X ...
- springboot整合Thymeleaf模板引擎
引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</ ...
- Springboot整合thymeleaf模板
Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...
- (二)springboot整合thymeleaf模板
在我们平时的开发中,用了很久的jsp作view显示层,但是标签库和JSP缺乏良好格式的一个副作用就是它很少能够与其产生的HTML类似.所以,在Web浏览器或HTML编辑器中查看未经渲染的JSP模板是非 ...
- 【Springboot】Springboot整合Thymeleaf模板引擎
Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...
- SpringBoot系列——Thymeleaf模板
前言 thymeleaf是springboot官方推荐使用的java模板引擎,在springboot的参考指南里的第28.1.10 Template Engines中介绍并推荐使用thymeleaf, ...
- SpringBoot使用thymeleaf模板引擎
(1).添加pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...
- springboot 引入 thymeleaf 模板
第一步pom中: <!-- 引入 thymeleaf 模板依赖 --> <dependency> <groupId>org.springframework.boot ...
- SpringBoot日记——Thymeleaf模板引擎篇
开发通常我们都会使用模板引擎,比如:JSP.Velocity.Freemarker.Thymeleaf等等很多,那么模板引擎是干嘛用的? 模板引擎,顾名思义,是一款模板,模板中可以动态的写入一些参数, ...
随机推荐
- springBoot 微服务
微服务,是OO (面向对象,Object Oriented) 专家 Martin Fowler 于2014年在他一篇文章<Microservice>提出的.在 Mattin 的头脑中,兴奋 ...
- 中矿大新生赛 G 甄总搬石头【优先队列/哈夫曼/贪心】
时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K,其他语言65536K64bit IO Format: %lld 题目描述 https://www.nowcoder.com/ ...
- PDF笔记:内嵌字体
前几天投文章的时候,把docx文件保存为PDF提交.但是格式检查始终在报一个关于“font embed”的错误,意思是PDF文件中有些字体没有内嵌. 为了减小文件大小,WORD保存为PDF的时候默认不 ...
- (转)Unity3D协同程序(Coroutine)
一.什么是协同程序 协同程序,即在主程序运行时同时开启另一段逻辑处理,来协同当前程序的执行.换句话说,开启协同程序就是开启一个线程. 二.协同程序的开启与终止 在Unity3D中,使用MonoBeha ...
- Using ASIHTTPRequest in an iOS project
1) Add the files Copy the files you need to your project folder, and add them to your Xcode project. ...
- JAVA常见算法题(八)
package com.xiaowu.demo; /** * 求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字.例如2+22+222+2222+22222(此时共有5个数相加), ...
- C++ 获取URL图片、html文件,CInternetSession 【转】
http://blog.csdn.net/gnixuyil/article/details/7688439 获取网络图片 CString URL="http://www.google.com ...
- spring aop expression支持多个表达式配置
<!-- 配置那些类的方法进行事务管理 --> <aop:config> <aop:pointcut id="allServiceMethod" ex ...
- scrapy爬虫 函数间传值简易教程
2017-03-27 有的时候我们爬取数据的时候需要在多个页面之间跳转,爬取完所有页面的数据的时候才能把所有数据一起存到数据库,这个时候我们就需要把某个函数内爬取的数据传到下一个函数当中.有人可能会说 ...
- SpringMVC文件上传的配置
记述一下步骤以备查. 准备工作: 需要把Jakarta Commons FileUpload及Jakarta Commons io的包放lib里. 我这边的包是: commons-fileupload ...