说实话,用起来很难受,但是人家官方推荐,咱得学

如果打成jar,这个就合适了,jsp需要容器支持

引入依赖

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

application.properties中配置

#指定模板所在的目录
spring.thymeleaf.prefix=/WEB-INF/ui/
#检查模板路径是否存在
spring.thymeleaf.check-template-location=true
#如果开启,本地调式页面不会立马更新,上线再打开缓存提高性能
spring.thymeleaf.cache=false
#模板文件后缀
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html

thymeleaf建的视图是HTML文件

新建一个testThymeleaf.html,这里简单使用几个属性,详细见手册

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"></meta>
<title>thymeleaf test</title>
</head>
<body>
<!-- text展示后台变量 -->
<h1 th:text="${message }">thymeleaf test</h1>
<!-- 字符串拼接,用|变量、文本| -->
<p th:text="|${message },welcome.|"></p>
<!-- 条件判断,if和unless 条件一致,情况不同显示不同的文本 -->
<span th:if="${aaa == 'aaa'}" th:text="uuuuuuuuuu"></span>
<span th:unless="${aaa == 'aaa'}" th:text="ggggggggggggggg"></span>
<!-- 循环迭代数据 -->
<table>
<tr>
<td>No.</td>
<td>姓名</td>
<td>日期</td>
</tr>
<tr th:each="each,iterStat : ${users}">
<td th:text="${iterStat.count}"></td>
<td>
<!-- 链接引用页面变量 -->
<a th:text="${each.name}" th:href="@{'/a/'+${each.name}+'/'+${iterStat.count}}">link</a>
</td>
<td th:text="${#dates.format(each.date, 'yyyy-MM-dd HH:mm:ss')}"></td>
</tr>
</table>
<a th:href="@{/a/hh/12}">test href</a>
</body>
</html>

后台示例

@Controller
public class TestThymeleafController { @GetMapping("/forward")
public String forward(ModelMap model){
model.addAttribute("message", "hello");
model.addAttribute("aaa", "aaac");
//直接写HTML文件的名字
return "testThymeleaf";
} @GetMapping("/user")
public String getUser(ModelMap model){
List<TestVO> users = new ArrayList<TestVO>();
TestVO vo = new TestVO();
vo.setName("小王");
vo.setDate(new Date());
users.add(vo);
model.addAttribute("users", users);
//直接写HTML文件的名字
return "testThymeleaf";
} @RequestMapping("/a/{p}/{v}")
@ResponseBody
public String a(@PathVariable("p") String p,@PathVariable("v") Integer v) {
return "test href:"+p+v;
}
}

spring boot thymeleaf简单示例的更多相关文章

  1. Spring Boot Thymeleaf 模板引擎的使用

    Spring Boot 中可以支持很多模板引擎,Thymeleaf 是 Spring Boot 官方推荐使用的模板引擎,虽然在社区 Thymeleaf 的性能被许多人所吐糟,但这仍然不影响大量的开发人 ...

  2. spring boot + Thymeleaf开发web项目

    "Spring boot非常适合Web应用程序开发.您可以轻松创建自包含的HTTP应用.web服务器采用嵌入式Tomcat,或者Jetty等.大多数情况下Web应用程序将使用 spring- ...

  3. Spring Boot Thymeleaf 实现国际化

    开发传统Java WEB工程时,我们可以使用JSP页面模板语言,但是在SpringBoot中已经不推荐使用了.SpringBoot支持如下页面模板语言 Thymeleaf FreeMarker Vel ...

  4. spring boot + thymeleaf 乱码问题

    spring boot + thymeleaf 乱码问题 hellotrms 发布于 2017/01/17 15:27 阅读 1K+ 收藏 0 答案 1 开发四年只会写业务代码,分布式高并发都不会还做 ...

  5. spring boot + thymeleaf 3 国际化

    在给spring boot 1.5.6 + thymeleaf 3进行国际化时,踩了一个坑(其实不止一个). 现象: 看到了吧, 就是取值的key, 后面被加了_en_US 或 _zh_CN, 以及前 ...

  6. Spring boot+Thymeleaf+easyui集成:js创建组件页面报错

    开发工具:Ideal 使用场景:Demo 前提:       环境:Spring boot +Thymeleaf+easyui 引入thymeleaf模板引擎 <html lang=" ...

  7. Spring Boot项目简单上手+swagger配置+项目发布(可能是史上最详细的)

    Spring Boot项目简单上手+swagger配置 1.项目实践 项目结构图 项目整体分为四部分:1.source code 2.sql-mapper 3.application.properti ...

  8. spring boot: thymeleaf模板引擎使用

    spring boot: thymeleaf模板引擎使用 在pom.xml加入thymeleaf模板依赖 <!-- 添加thymeleaf的依赖 --> <dependency> ...

  9. Spring boot 注解简单备忘

    Spring boot 注解简单备忘 1.定义注解 package com.space.aspect.anno;import java.lang.annotation.*; /** * 定义系统日志注 ...

随机推荐

  1. fiddler对浏览器、app抓包及证书安装

    1.fiddler对浏览器抓包 1.1 对浏览器的http的抓包 Capturing开启,进行抓包: Capturing关闭,停止抓包: 如下图:  1.2 对浏览器的https抓包 1.2.1 开启 ...

  2. 剑指offer第二版面试题4:替换空格(JAVA版)

    题目:请实现一个函数,把字符串中的每个空格替换成“%20”.例如输入“We are happy”,则输出”We%20are%20happy”. 原因:在网络编程中,如果URL参数中含有特殊字符,如:空 ...

  3. 2018 ECNA Regional Contest J. Watch Where You Step

    题目链接:Watch Where You Step 题意 给定有向图的邻接矩阵,现在需要给该图增加边,使得如果两点可达必直接可达,求需要加边的数量. 题解 首先,如果给定 \(n\) 个结点的图中任意 ...

  4. LeetCode 最小栈

    题目链接:https://leetcode-cn.com/problems/min-stack/ 题目大意 略.并且题目中要求的操作都要 O(1) 实现. 分析 用 2 个栈,一个普通栈,一个单调栈. ...

  5. C#基础-->cookie和session

    关于cookie和session cookie 1:一个cookie中可以存放的数据最大在4KB左右 2:cookie存放于客户端 3:cookie分为两种  一种是会话cookie  一种是持久co ...

  6. Jackson环境安装设置

    本地环境设置 由于Jackson是基于Java编程语言的,所以需要设置Java开发环境,这里介绍如何下载安装设置Java.请按照以下步骤来设置环境. Java SE是免费的,点击下载链接:下载Java ...

  7. Vue引入日期格式插件moment.js

    因为需求需要,接口传递过来的日期格式是一个时间戳,因此需要进行格式转换,老大给了插件地址,让我自己研究 插件地址:http://momentjs.cn/ 因为没有使用过,所有就开始各种百度,参考各位大 ...

  8. 利用left join 筛选B表中不包含A表记录

    select A.key from A LEFT JOIN B ON A.KEY=B.KEY WHERE B.FIELD IS NULL;

  9. layerui ios不适应问题

    .admin-main {-webkit-overflow-scrolling: touch; overflow: scroll; position: absolute; left: 0; top:  ...

  10. volatile的使用及其原理

    1. volatile的作用 相比Sychronized(重量级锁,对系统性能影响较大),volatile提供了另一种解决 可见性和有序性 ???问题的方案.对于原子性,需要强调一点,也是大家容易误解 ...