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

如果打成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. Spring源码剖析3:Spring IOC容器的加载过程

    本文转自五月的仓颉 https://www.cnblogs.com/xrq730 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https ...

  2. Openfire调整成自己的IM部署到LInux系统上

    部署自己的IM到Linux系统下:需要生成相关jar包再部署 1.在myeclipse上把openfire源代码加在加载进来,做相应调整后,运行测试没问题 2.  编译admin-jsp.jar(所有 ...

  3. cgroup & oom-killer 简介

    cgroup内存限制 memory.failcnt memory.limit_in_bytes memory.usage_in_bytes memory.max_usage_in_bytes memo ...

  4. XML 扩展部分

    引入命名空间 xmlns DTD缺点 1.不支持命名空间 2.支持的数据类型很少 3.DTD不可扩展 4.DTD不遵循XML规范 DTD的优点 简洁 schema 通过schema来解决DTD的不足 ...

  5. 机器学习基石笔记:Homework #4 Regularization&Validation相关习题

    原文地址:https://www.jianshu.com/p/3f7d4aa6a7cf 问题描述 程序实现 # coding: utf-8 import numpy as np import math ...

  6. linux更新grub内核启动参数的方法

    #!/bin/bash set -x set -e export PS4=+{$LINENO:${FUNCNAME[0]}} trap 'echo "---NEWKERNARGS=$NEWK ...

  7. leetcode.数组.283移动零-Java

    1. 具体题目 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须 ...

  8. 三(1)、springcloud之Eureka服务注册与发现

    1.认识Eureka ​ Eureka是Netflix的一个子模块,也是核心模块之一.Eureka是一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移.服务注册与发现对于微服务架 ...

  9. C# Winform DotNetBar控件之StyleManager

    这个控件作用改变窗体样式 使用方法 拖拽一个styleManager到窗体 改变它的managerStyle,再把窗体的继承改成Office2007Form 最后一步窗体初始化控件前 改掉的Enabl ...

  10. PHP之如何编写一个Vue的API后台(一)

    首先我们先建立文件的结构 如下图: components - 存放所有的全局方法,比如:autoplay的函数 lib - 所有第三方的方法  比如:DBTool:数据库的方法 logs -  日志 ...