之前搞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例子的更多相关文章

  1. 第一篇 Springboot + Web MVC + MyBatis + 简单UI + Thymeleaf实现

    源码链接:https://pan.baidu.com/s/1-LtF56dnCM277v5lILRM7g 提取码:c374 第二篇 Springboot mybatis generate根据数据库表自 ...

  2. SpringBoot入门 一 构建简单工程

    环境准备:jdk1.7(推荐)以上,tomcat8(推荐)以上,或者使用插件自带.mevan插件3.2以上,eclipse编辑工具 pom文件基本配置如下 <project xmlns=&quo ...

  3. Java结合SpringBoot拦截器实现简单的登录认证模块

    Java结合SpringBoot拦截器实现简单的登录认证模块 之前在做项目时需要实现一个简单的登录认证的功能,就寻思着使用Spring Boot的拦截器来实现,在此记录一下我的整个实现过程,源码见文章 ...

  4. SpringBoot入门系列(五)Thymeleaf的常用标签和用法

    前面介绍了Spring Boot 中的整合Thymeleaf .不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/16577 ...

  5. SpringBoot thymeleaf使用方法,thymeleaf模板迭代

    SpringBoot thymeleaf使用方法,thymeleaf模板迭代 SpringBoot thymeleaf 循环List.Map ============================= ...

  6. springBoot整合MyBatise及简单应用

    springBoot整合MyBatise及简单应用 我采用的是 工具IDEA 框架是springBoot+maven+Mybatise 第一步: pom.xml 引入相关jar包 <?xml v ...

  7. springboot集成模板引擎freemarker和thymeleaf

    freemarkder和thymeleaf都是java的模板引擎,这里只介绍这两种模板引擎如何在sprongboot中配置: 1. freemarkder 1.1 在pom.xml中添加依赖包 < ...

  8. springboot微服务的简单小结

    springboot微服务的简单小结 近来公司用springboot微服务,所以小结一下. 基础: 什么是SpingBoot微服务? 如何创建SpringBoot微服务? 如何管理和完善SpringB ...

  9. SpringBoot基于数据库实现简单的分布式锁

    本文介绍SpringBoot基于数据库实现简单的分布式锁. 1.简介 分布式锁的方式有很多种,通常方案有: 基于mysql数据库 基于redis 基于ZooKeeper 网上的实现方式有很多,本文主要 ...

  10. 超详细,新手都能看懂 !使用SpringBoot+Dubbo 搭建一个简单的分布式服务

    来自:JavaGuide Github 地址:https://github.com/Snailclimb/springboot-integration-examples 目录: 使用 SpringBo ...

随机推荐

  1. Failed to read artifact descriptor for org.apache.httpcomponents:httpmime:jar

    额,在Stackoverflow上找到了一个答案: I had this in eclipse and did this which fixed it(even though my command l ...

  2. JQuery不能加载click事件的问题

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  3. Exception in thread "main" java.util.ConcurrentModificationException

    package test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public c ...

  4. shiro自定义拦截url

    在实际项目上,我们针对不同的用户(guste,user,admin,mobile user)等等,需要进入不同的页面,比如,手机端用户需要进入Mobile/这个路径下的,这个时候,我们需要自定义拦截u ...

  5. Xcode 5、Xcode 6 免证书真机调试

    我们都知道,在iOS开发中,假设要进行真机调试的话是须要苹果开发人员账号的.否则Xcode就不可以进行真机调试.仅仅可以在模拟器上执行:这就带来了非常多问题,比方iCloud编程的话你不可以用模拟器. ...

  6. zendstudio的安装和破解

    参考博客地址:http://www.oxox.work/web/php-basic/zendstudio/ 注明:还未验证

  7. urllib库利用cookie实现模拟登录慕课网

    思路 1.首先在网页中使用账户和密码名登录慕课网 2.其次再分析请求头,如下图所示,获取到请求URL,并提取出cookie信息,保存到本地 3.最后在代码中构造请求头,使用urllib.request ...

  8. USB-HID鼠标、键盘通讯格式(转) 与本人实际测试结果

    内容为网络转载,如有版权问题请联系删除 USB鼠标键盘协议介绍. 鼠标发送给PC的数据每次4个字节:BYTE1 BYTE2 BYTE3 BYTE4.定义分别是:BYTE1 -- |--bit7:    ...

  9. 【BZOJ1000】A+B Problem ★BZOJ1000题达成★

    [BZOJ1000]A+B Problem Description 输入两个数字,输出它们之和 Input 一行两个数字A,B(0<=A,B<100) Output 输出这两个数字之和 S ...

  10. 【BZOJ4184】shallot 线段树+vector+线性基

    [BZOJ4184]shallot Description 小苗去市场上买了一捆小葱苗,她突然一时兴起,于是她在每颗小葱苗上写上一个数字,然后把小葱叫过来玩游戏. 每个时刻她会给小葱一颗小葱苗或者是从 ...