SpringBoot集成Thymeleaf模板
1、添加起步依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、添加配置(application.properties):
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
参考ThymeleafProperties.java类,cache默认为true,其它属性都有默认值,一般不需要修改。
3、写测试controller返回页面并携带数据:
package cn.mmweikt.es.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class IndexController {
@GetMapping("/index")
public String indexPage(Model model) {
model.addAttribute("name", "es_project.");
return "index";
}
}
4、在resources/templates下放.html页面,在resources/static下放静态文件。templates和static下都可以再放文件夹:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<span th:text="${name}"></span>
</body>
</html>
注意,在html标签里一定要引入 xmlns:th="http://www.thymeleaf.org" ,这样thymeleaf模板才会启用,才能使用th:*这样的语法。
语法可参考:
https://www.cnblogs.com/jin-zhe/p/8202885.html
https://www.cnblogs.com/nfcm/p/7843935.html
补充:
网上看到说,在spring-boot1.4之后,支持thymeleaf3,可以更改版本号来进行修改支持。3相比2极大的提高了效率,并且不需要标签闭合,类似的link,img等都有了很好的支持,按照如下配置即可:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- set thymeleaf version -->
<thymeleaf.version>3.0.0.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.0</thymeleaf-layout-dialect.version>
<!--set java version-->
<java.version>1.8</java.version>
</properties>
SpringBoot集成Thymeleaf模板的更多相关文章
- 九、SpringBoot集成Thymeleaf模板引擎
Thymeleaf咋读!??? 呵呵,是不是一脸懵逼...哥用我的大学四级英文知识告诉你吧:[θaimlif]. 啥玩意?不会音标?...那你就这样叫它吧:“赛母李府”,大部分中国人是听不出破绽的.. ...
- SpringBoot集成Thymeleaf模板引擎
简单介绍 目前在JavaEE领域有几中比较常用的模板引擎,分别是Jsp.Velocity.Freemarker.Thymeleaf,对Freemark语法不是特别熟悉,不过对于前端页面渲染效率来说,j ...
- SpringBoot 之Thymeleaf模板.
一.前言 Thymeleaf 的出现是为了取代 JSP,虽然 JSP 存在了很长时间,并在 Java Web 开发中无处不在,但是它也存在一些缺陷: 1.JSP 最明显的问题在于它看起来像HTML或X ...
- Springboot 集成 Thymeleaf 及常见错误
Thymeleaf模板引擎是springboot中默认配置,与freemarker相似,可以完全取代jsp,在springboot中,它的默认路径是src/main/resources/templat ...
- springboot整合Thymeleaf模板引擎
引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</ ...
- Springboot整合thymeleaf模板
Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...
- SpringBoot集成beetl模板快速入门
SpringBoot集成beetl模板快速入门 首次探索 beetl官方网址:http://ibeetl.com/ 创建SpringBoot工程(idea) 新建工程 选择创建Spring工程 书写包 ...
- SpringBoot集成thymeleaf(自定义)模板中文乱码的解决办法
楼主今天在学习SpringBoot集成thymelaf的时候报了中文乱码的错误,经过网上的搜索,现在得到解决的办法,分享给大家: package com.imooc.config; import or ...
- springboot用thymeleaf模板的paginate分页
本文根据一个简单的user表为例,展示 springboot集成mybatis,再到前端分页完整代码(新手自学,不足之处欢迎纠正): 先看java部分 pom.xml 加入 <!--支持 Web ...
随机推荐
- 如何获得一个干净的 gnome 开发环境?
下载 stage3-amd64-systemd-xxxxxxxx.tar.bz2 eselect profile set default/linux/amd64/17.0/desktop/gnome/ ...
- lua脚本分解字符串
--local str = "文字45 文字 789 文们adsd45 文字 wowo 文字 文字 wowo我们 wowo456 wiwo 465我们 456sdf 45 45我们adsd4 ...
- Angular的一些常用命令
Angular的一些常用命令 cmd中创建项目: ng new taskmgr -si --style=scss //先不安装依赖,si 为skip install material需要scss样式的 ...
- python实现WordCount(第三次作业)
0x00 注明 合作者:201631062315 201631062310 代码地址:https://gitee.com/c1e4r/word-count2 作业地址:https://edu.cnbl ...
- 循环结构select 举例
- docker 安装cat
1.下载cat cat 地址:https://github.com/dianping/cat 进入opt 创建cat文件夹 cd /opt/ mkdir cat cd cat 下载cat git cl ...
- go语言从例子开始之Example15.闭包
Go 支持通过 闭包来使用 匿名函数.匿名函数在你想定义一个不需要命名的内联函数时是很实用的. 闭包简单理解,函数反回值是一个函数 Example: package main import " ...
- 力扣——3sum closest(最接近的三数之和)python 实现
题目描述: 中文: 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一 ...
- vscode 常用的插件
这些是本人在使用vscode中用的比较爽的插件,个人爱好习惯不同,请按需拿取.先声明本人是一个前端,所用的,插件都是和前端匹配的,后台的同学可以不用浪费时间了 基础插件 chinese 英文是所有读书 ...
- poj 1144 割点模板题
题目大意: 求割点: 基本思路: tarjan算法,套板子 代码如下: #include<iostream> #include<string> #include<vect ...