分享知识-快乐自己:springboot之thymeleaf (1):简单的thymeleaf例子
之前搞springboot时,发现spring很推荐thymeleaf,所以看了看学了学,感觉不错,做个笔记先。
做个简单和例子,项目是springboot,所以引入themeleaf相关包
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
接着写个controller

@RequestMapping("/")
public String hello(Model model, HttpServletRequest request) {
// 文本表达式
model.addAttribute("name", "ywj");
// 获取对象属性
model.addAttribute("user", new User("ywj3"));
// 获取session文本
request.getSession().setAttribute("sname", "name from session");
// 获取session对象属性
request.getSession().setAttribute("suser", new User("name from session"));
// 显示html效果
model.addAttribute("html", "<b>BBB</b>");
// 数组
String[] arr = {"a", "b"};
model.addAttribute("arr", arr);
// url
model.addAttribute("url", "http://www.baidu.com");
model.addAttribute("today", new Date());
return "index";
}

接着在src/main/resources/templates中创建一个index.html,是html,不是jsp 然后看看thymeleaf语法:

///
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Hello Thymeleaf!</title>
</head>
<body>
文本表达式:
<span th:text="${name}" /><hr/>
文本表达式,获取对象属性:
<span th:text="${user.name}" /><hr/>
session中文本:
<span th:text="${session.sname}" /><hr/>
session中对象属性:
<span th:text="${session.suser.name}" /><hr/>
html效果:
<span th:utext="${html}" /><hr/>
url:
<a th:href="@{${url}}">baidu</a><hr/>
url跳转
<a th:href="@{/b}">效果是:127.0.0.1/项目名/b</a><hr/>
url还参数,效果:/c?a=aaa&b=bbb.还有一种@{/c/{a}(a='aaa'))} = /c/aaa
<a th:href="@{/c(a='aaa',b='bbb')}">url带参数</a><hr/>
url同一服务器下请求别的项目,比如项目A和项目thymeleaf1是同一个服务器里页的,项目A请求项目thymeleaf1里的方法,就用~/thymeleaf1/方法
<a th:href="@{~/thymeleaf1/c(a='aaa',b='bbb')}">url带参数</a><hr/>
数字可加减乘除余
<span th:text="1" />加:1+2=<span th:text="${1+2}" />减:2-1=<span th:text="${2-1}" />
乘:2*3=<span th:text="${2*3}" />除:6/2=<span th:text="${6/2}" />余:3%2=<span th:text="${3%2}" /><hr/>
数字比较:<!-- gt(>),lt(<),ge(>=),le(<=),not(!) eq(==),neq/ ne(!=) -->
<span th:if="${2 > 1}">2大于1</span>
<span th:if="2 < 1">2大于1</span>
时间格式:
<span th:text="${#dates.format(today, 'yyyy-MM-dd HH:mm:ss')}">13 May 2011</span>
</body>
</html>///

(注:更多详解请跳转:https://blog.csdn.net/u013845177/article/category/7226505)
分享知识-快乐自己:springboot之thymeleaf (1):简单的thymeleaf例子的更多相关文章
- 第一篇 Springboot + Web MVC + MyBatis + 简单UI + Thymeleaf实现
源码链接:https://pan.baidu.com/s/1-LtF56dnCM277v5lILRM7g 提取码:c374 第二篇 Springboot mybatis generate根据数据库表自 ...
- SpringBoot入门 一 构建简单工程
环境准备:jdk1.7(推荐)以上,tomcat8(推荐)以上,或者使用插件自带.mevan插件3.2以上,eclipse编辑工具 pom文件基本配置如下 <project xmlns=&quo ...
- Java结合SpringBoot拦截器实现简单的登录认证模块
Java结合SpringBoot拦截器实现简单的登录认证模块 之前在做项目时需要实现一个简单的登录认证的功能,就寻思着使用Spring Boot的拦截器来实现,在此记录一下我的整个实现过程,源码见文章 ...
- SpringBoot入门系列(五)Thymeleaf的常用标签和用法
前面介绍了Spring Boot 中的整合Thymeleaf .不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/16577 ...
- SpringBoot thymeleaf使用方法,thymeleaf模板迭代
SpringBoot thymeleaf使用方法,thymeleaf模板迭代 SpringBoot thymeleaf 循环List.Map ============================= ...
- springBoot整合MyBatise及简单应用
springBoot整合MyBatise及简单应用 我采用的是 工具IDEA 框架是springBoot+maven+Mybatise 第一步: pom.xml 引入相关jar包 <?xml v ...
- springboot集成模板引擎freemarker和thymeleaf
freemarkder和thymeleaf都是java的模板引擎,这里只介绍这两种模板引擎如何在sprongboot中配置: 1. freemarkder 1.1 在pom.xml中添加依赖包 < ...
- springboot微服务的简单小结
springboot微服务的简单小结 近来公司用springboot微服务,所以小结一下. 基础: 什么是SpingBoot微服务? 如何创建SpringBoot微服务? 如何管理和完善SpringB ...
- SpringBoot基于数据库实现简单的分布式锁
本文介绍SpringBoot基于数据库实现简单的分布式锁. 1.简介 分布式锁的方式有很多种,通常方案有: 基于mysql数据库 基于redis 基于ZooKeeper 网上的实现方式有很多,本文主要 ...
- 超详细,新手都能看懂 !使用SpringBoot+Dubbo 搭建一个简单的分布式服务
来自:JavaGuide Github 地址:https://github.com/Snailclimb/springboot-integration-examples 目录: 使用 SpringBo ...
随机推荐
- two sum, three sum和four sum问题
1. two sum问题 给定一组序列:[-4 -6 5 1 2 3 -1 7],然后找出其中和为target的一对数 简单做法:两层循环遍历,时间复杂度为n^2 升级版:对给定的序列建立一个hash ...
- 对象复制帮助类---DeepCopy
有的时候我们在对一个引用类型的对象进行传递操作的时候希望不要直接修改传递过来的对象,而是复制出一份来操作的时候就可以用下面的类进行复制 sing System.IO; using System.Run ...
- ASIHTTPRequest中文入门教程全集 http://www.zpluz.com/thread-3284-1-1.html
本文转载至 目录 3 第 1 章 创建和运行请求 5 1.1. 创建一个同步请求 5 1.2. 创建一个异步请求 5 1.3. 使用程序块(blocks ) 6 1.4. 使用 ...
- CSS3 线性渐变(linear-gradient)
CSS3 Gradient 分为 linear-gradient(线性渐变)和 radial-gradient(径向渐变).而我们今天主要是针对线性渐变来剖析其具体的用法.为了更好的应用 CSS3 G ...
- 素数定理 nefu 117
素数定理: 随着x的增长,P(x) ≍x/ln(x) ,P(x)表示(1,x)内的素数的个数. 这个定理,说明在1-x中,当x大到一定程度时,素数分布的概率为ln(x) 竟然还有一道题目. 素数个数的 ...
- [精]poj2724
Purifying Machine Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5027 Accepted: 1455 ...
- EasyNVR无插件H5/HLS/m3u8直播解决方案中Windows系统服务启动错误问题的修复:EasyNVR_Service 服务因 函数不正确。 服务特定错误而停止。
最近在做某地市移动公司景观直播的项目时,遇到一个问题,当我们部署EasyNVR为系统服务后,居然出现了无法启动服务的现象,表面上看,提示是系统服务启动失败,实际通过查看windows 系统日志: 查找 ...
- [ZJOI2006]三色二叉树
[ZJOI2006]三色二叉树 BZOJ luogu 分3种颜色讨论转移一下 #include<bits/stdc++.h> using namespace std; const int ...
- Android N API预览
Android N for Developers 重要的开发人员功能 多窗体支持 通知 JIT/AOT 编译 高速的应用安装路径 外出瞌睡模式 后台优化 Data Saver 高速设置图块 API 号 ...
- sqlalchemy——多表操作
一对多:一对一 # one -- many class Students(Base): __tablename__ = "students" sid = Column(Intege ...