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

如果打成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. php 空格,换行,跳格使用说明

    首先说说\n,\r,\t \n 软回车: 在Windows 中表示换行且回到下一行的最开始位置 在Linux.unix 中只表示换行,但不会回到下一行的开始位置. \r 软空格: 在Linux.uni ...

  2. (转)openfire插件开发(一)

    转:http://blog.csdn.net/lovexieyuan520/article/details/37774909 1. 在上一篇博文中,我介绍了Openfire3.9.3源代码导入ecli ...

  3. js滚动到顶部底部代码

    <!DOCTYPE HTML> <html> <head> <meta charset=UTF-8> <title>SCROLL</t ...

  4. 与DSP通信时,RD&WR信号

      /////////////////////////////////////////////////////////// :] rd,wr; :] dsp_data_out; 'hzzzz; // ...

  5. WPF 3D

    参考MSDN内容:http://msdn.microsoft.com/zh-cn/library/ms747437(v=vs.110).aspx 概述 WPF使用Viewport3D元素,将三维场景显 ...

  6. Git 学习第二天(一)

    继续昨天的学习. 回顾一下,昨天我们安装了git 并创建了登录名及邮箱,还向git仓库提交了一个readme.txt的文本文件 下面,我们来修改下这个文件,将内容改为 Git is a distrib ...

  7. 如何设置linux启动过程中的停止阶段

    设置方法: 1 启动过程中点击“e”键(fedora)或者"tab"(centOS)键进入目标启动项的命令行参数下,移除initrd所在行末尾的"quiet" ...

  8. 【POJ】2031 Building a Space Station

    题目链接:http://poj.org/problem?id=2031 题意:修建太空站每个舱之间的走廊.每个舱都是球体.给出n个舱的三维空间坐标以及球体半径.如果球体之间接触或者相接,就不用修走廊. ...

  9. jq容易混淆点

    jQuery链式编程: 原:把当前元素颜色修改为红色,其余兄弟颜色不变 $(this).css("color",'red') ; $(this).siblings().css(&q ...

  10. ElementUI的Loading组件 —— 想实现在请求后台数据之前开启Loading组件,请求成功或失败之后,关闭Loading组件

    我在实际项目开发中,遇到了这个需求,记录一下~~~~~~ 在ElementUI官网上有几种实现Loading的方法,但官网上是在一个方法里写了开启与关闭组件,所以可以根据官网的实现方法进行一个封装,便 ...