spring boot thymeleaf简单示例
说实话,用起来很难受,但是人家官方推荐,咱得学
如果打成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简单示例的更多相关文章
- Spring Boot Thymeleaf 模板引擎的使用
Spring Boot 中可以支持很多模板引擎,Thymeleaf 是 Spring Boot 官方推荐使用的模板引擎,虽然在社区 Thymeleaf 的性能被许多人所吐糟,但这仍然不影响大量的开发人 ...
- spring boot + Thymeleaf开发web项目
"Spring boot非常适合Web应用程序开发.您可以轻松创建自包含的HTTP应用.web服务器采用嵌入式Tomcat,或者Jetty等.大多数情况下Web应用程序将使用 spring- ...
- Spring Boot Thymeleaf 实现国际化
开发传统Java WEB工程时,我们可以使用JSP页面模板语言,但是在SpringBoot中已经不推荐使用了.SpringBoot支持如下页面模板语言 Thymeleaf FreeMarker Vel ...
- spring boot + thymeleaf 乱码问题
spring boot + thymeleaf 乱码问题 hellotrms 发布于 2017/01/17 15:27 阅读 1K+ 收藏 0 答案 1 开发四年只会写业务代码,分布式高并发都不会还做 ...
- spring boot + thymeleaf 3 国际化
在给spring boot 1.5.6 + thymeleaf 3进行国际化时,踩了一个坑(其实不止一个). 现象: 看到了吧, 就是取值的key, 后面被加了_en_US 或 _zh_CN, 以及前 ...
- Spring boot+Thymeleaf+easyui集成:js创建组件页面报错
开发工具:Ideal 使用场景:Demo 前提: 环境:Spring boot +Thymeleaf+easyui 引入thymeleaf模板引擎 <html lang=" ...
- Spring Boot项目简单上手+swagger配置+项目发布(可能是史上最详细的)
Spring Boot项目简单上手+swagger配置 1.项目实践 项目结构图 项目整体分为四部分:1.source code 2.sql-mapper 3.application.properti ...
- spring boot: thymeleaf模板引擎使用
spring boot: thymeleaf模板引擎使用 在pom.xml加入thymeleaf模板依赖 <!-- 添加thymeleaf的依赖 --> <dependency> ...
- Spring boot 注解简单备忘
Spring boot 注解简单备忘 1.定义注解 package com.space.aspect.anno;import java.lang.annotation.*; /** * 定义系统日志注 ...
随机推荐
- ubuntu ceph集群安装以及简单使用
ubuntu ceph安装以及使用 1.安装环境 本文主要根据官方文档使用ubuntu14.04安装ceph集群,并且简单熟悉其基本操作.整个集群包括一个admin节点(admin node,主机名为 ...
- 3.1 开始使用 redux
前面我们介绍了 flux 架构以及其开源实现 redux,在这一节中,我们将完整的介绍 redux: redux 介绍 redux 是什么 redux 概念 redux 三原则 redux Store ...
- 3.2 Redux TodoApp
上一节讲完了 redux 中的概念,但是仍然没有和 react 联系起来,这一节将利用 redux 在 react 中实现完整的 todolist: 在 react 使用 redux 通过 Provi ...
- 利用mysql数据库日志文件获得webshell
查看配置 show variables like '%general%'; 开启日志功能 set GLOBAL general_log='ON'; 设置日志存储路径 SET GLOBAL genera ...
- C-Ubuntu中MySQL出现ERROR1698(28000):Access denied for user root@localhost错误解决方法
卸载原先的MySQL,重新安装过程中没有提示设置密码, 当键入命令:mysql -uroot -p; 不知密码是什么,输入主机的密码以及不输入密码,回车后都出现标题错误. 寻找多篇相关博文,验证如下博 ...
- 欧拉筛 线性筛 素数+莫比乌斯的mu[]
https://blog.csdn.net/qq_39763472/article/details/82428602 模板来自https://blog.csdn.net/Avalon_cc/artic ...
- java.sql.SQLException: ORA-64203: 目标缓冲区太小, 无法容纳字符集转换之后的 CLOB 数据
<!--获取ae45at--> <select id="selectAe45at" parameterClass="java.util.Map" ...
- jsonp的实现原理
jsonp的实现原理 由于浏览器的安全性(同源策略)限制,不允许AJAX访问协议不同或域名不同或端口号不同的数据接口: 可以通过动态创建script标签的方式,把script标签的src属性指向数据接 ...
- TOP和PS aux命令显示出来的栏目所代表的意思
USER: 行程拥有者 PID: pid %CPU: 占用的 CPU 使用率 %MEM: 占用的记忆体使用率 VSZ: 占用的虚拟记忆体大小 RSS: 占用的记忆体大小 TTY: 终端的次要装置号码 ...
- 随笔记录 误删boot恢复 2019.8.7
系统还原: 1. 2. 3. 4. 5.进入硬盘 6.挂载光盘,安装恢复boot 7.安装grub2 8.重建grub.cfg文件