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

如果打成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. strlen、strcpy和strcmp源码

    1.不使用库函数实现strcpy #include <assert.h> char *strcpy(char *dst, const char *src) { assert((dst != ...

  2. 从内部重启python脚本

    我有一个基于python的GTK应用程序,可以加载几个模块.它是从(linux)终端运行的,如下所示: ./myscript.py --some-flag setting 用户可以从程序中下载(使用G ...

  3. 第七篇 css选择器实现字段解析

    CSS选择器的作用实际和xpath的一样,都是为了定位具体的元素 举例我要爬取下面这个页面的标题 In []: title = response.css(".entry-header h1& ...

  4. ArcGis Python常用脚本

    ArcGis Python脚本——ArcGIS 中使用的 Python 是什么版本 ArcGis Python脚本——批量添加字段 ArcGis Python脚本——批量删除字段 ArcGis Pyt ...

  5. Linux 父进程发送信号杀死子进程

    #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <signal. ...

  6. VMware1设备与主机共享网络的问题

    问题的提出: 最近需要用到VMware1设备来配置网络,顺便将VMware1设备与主机进行共享网络,这样master就能直接访问网络了,但是原本以为直接在wlan设备上选择网络共享就行了,但是却没法收 ...

  7. linux redis的启动---后台启动

    1.启动redis服务: redis-server 如果想要开启后台进程: 1.找到redis.conf里边的 把no 改为yes. 2.redis-server redis.conf(这个是针对两个 ...

  8. h5 input无法输入问题 屏蔽长按事件

    开发h5 app中突然发现在手机上长按文本会出现复制粘贴菜单,只要是文本长按都会出现这种情况确实有些不太符合交互,为此特意去翻了一下博客,得到了已下解决方案: 将所有元素的系统默认菜单禁用掉 *{ - ...

  9. hadoop.io.native.NativeID$Windows.access0 报错问题解决

    系统:win10 hadoop-2.6.0版本 java:1.8 版本32位   wordcount在本地运行时报错: Exception in thread "main" jav ...

  10. Delphi Format函数功能及用法详解

    DELPHI中Format函数功能及用法详解 DELPHI中Format函数功能及用法详解function Format(const Format: string; const Args: array ...