分享知识-快乐自己: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 ...
随机推荐
- protobuf编译安装
为什么选择protobuf,而不选择thift和avro,原因大概几点吧,网上对比的文章很多,我主要关注以下几点 1.protobuf序列化性能最好,序列化后字节数最少. 2.protobuf是单纯的 ...
- (1)安装kvm
我的环境是redhat虚拟机,版本信息如下: [root@localhost ~]# cat /etc/issue Red Hat Enterprise Linux Server release 6. ...
- 【SQLServer2008】之如何导入mdf,ldf文件、导入.mdf文件报错解决问题以及如何获得Authenticated Users 权限
一.如何导入mdf,ldf文件: 链接:http://jingyan.baidu.com/article/09ea3ede21258cc0afde3943.html 二.导入.mdf文件报错解决问题 ...
- java程序优化
程序代码优化要点: 字符串优化:分析String源码,了解String常用方法,使用StringBuffer.StringBuilder. List.Map.Set优化:分析常用ArrayList.L ...
- 删除路径下的文件以及子路径。(范例“E:/www/”)
/// <summary> /// 删除路径下的文件以及子路径. /// </summary> /// <param name="dir">目标 ...
- Django 之Form组件
Django之From组件 扩展:Django 之 ModelForm组件 Form组件功能 Django的Form主要具有一下几大功能 生成HTML标签 验证用户数据(显示错误信息) HTML Fo ...
- Linux kernel config and makefile system
转载自:http://blog.csdn.net/dreamxu/article/details/6125545 http://www-900.ibm.com/developerWorks/cn/li ...
- https 请求发送 例子 tls && ssl
package com.dooioo.training.helper; import java.io.IOException; import java.io.UnsupportedEncodingEx ...
- Redis持久化——问题定位与优化(三)
核心知识点: 1.fork操作 a.在RDB或AOF重写时,会执行fork操作创建子进程,fork操作是一个重量级操作. b.改善fork操作耗时的手段:避免使用Xen.配置Redis实例最大使用内存 ...
- 纯CSS3垂直动画菜单
在线演示 本地下载