目录:
(1)字符串连接
(2)运算符
(3)Thymeleaf基本对象-内置对象使用
(4)内置对象#request、#session的方法
(5) 一些内置对象事例
(1)字符串连接
控制器:ThemeleafController:
package com.bjpowernode.controller;
import com.bjpowernode.model.SysUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/tpl") //放在类上方的这个注解会和@GetMapping一起组成完整的访问路径
public class ThymeleafController {
//字符串连接
@GetMapping("/strjoin")
public String strjoin(Model model){
model.addAttribute("sex","m");
model.addAttribute("age",20);
model.addAttribute("name","张三");
model.addAttribute("city","北京");
model.addAttribute("isLogin",true);
model.addAttribute("myuser",new SysUser(1005,"周峰","男",23));
return "strjoin";
}
}
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>index.html--学习Thymeleaf语法</h3>
<a href="tpl/expression1">标准变量表达式</a>
<br>
<a href="tpl/expression2">选择变量表达式</a>
<br>
<a href="tpl/link">连接表达式</a>
<br>
<a href="tpl/property">模板属性</a>
<br>
<a href="tpl/eachList">循环list集合</a>
<br>
<a href="tpl/eachArray">循环数组Array</a>
<br>
<a href="tpl/eachMap">循环Map集合</a>
<br>
<a href="tpl/ifunless">判断语句if和unless</a>
<br>
<a href="tpl/switch">判断语句switch</a>
<br>
<a href="tpl/inline">内联iniline</a>
<br>
<a href="tpl/text">字面量</a>
<br>
<a href="tpl/strjoin">字符串连接</a>
</body>
</html>
模板strjoin.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div style="margin-left: 400px">
<h3>字符串的连接方式1:使用单引号括起来的字符串</h3>
<p th:text="'我是'+${name}+',我所在的城市'+${city}">数据显示</p>
<h3>字符串连接方式2:|字符串和表达式|</h3>
<p th:text="|我是${name},所在城市${city},其他人${myuser.name}"></p>
</div>
</body>
</html>
重启主启动类:在浏览器输入地址访问:
点击字符串连接:
(2)运算符
控制类:
package com.bjpowernode.controller;
import com.bjpowernode.model.SysUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/tpl") //放在类上方的这个注解会和@GetMapping一起组成完整的访问路径
public class ThymeleafController {
//运算符的使用
@GetMapping("/sym")
public String sym(Model model){
model.addAttribute("sex","m");
model.addAttribute("age",20);
model.addAttribute("name","张三");
model.addAttribute("city","北京");
model.addAttribute("isLogin",true);
model.addAttribute("myuser",null);
return "sym";
}
}
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>index.html--学习Thymeleaf语法</h3>
<a href="tpl/expression1">标准变量表达式</a>
<br>
<a href="tpl/expression2">选择变量表达式</a>
<br>
<a href="tpl/link">连接表达式</a>
<br>
<a href="tpl/property">模板属性</a>
<br>
<a href="tpl/eachList">循环list集合</a>
<br>
<a href="tpl/eachArray">循环数组Array</a>
<br>
<a href="tpl/eachMap">循环Map集合</a>
<br>
<a href="tpl/ifunless">判断语句if和unless</a>
<br>
<a href="tpl/switch">判断语句switch</a>
<br>
<a href="tpl/inline">内联iniline</a>
<br>
<a href="tpl/text">字面量</a>
<br>
<a href="tpl/strjoin">字符串连接</a>
<br>
<a href="tpl/sym">运算符的使用</a>
</body>
</html>
模板sym.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div style="margin-left: 400px">
<h3>使用运算符</h3>
<p th:text="${age>10}">年龄大于10</p>
<p th:text="${20+30}">显示运算结果</p>
<P th:if="${myuser == null}">myuser是null</P>
<p th:if="${myuser eq null}">myuser是null</p>
<!--ne不等于 不成立不显示-->
<p th:if="@{myuser ne null}">myuser不是null</p>
<p th:text="${isLogin==true ? '用户已经登录' : '用户需要登录'}"></p>
<h3>三元运算符嵌套:</h3>
<p th:text="${isLogin==true ? (age>10 ? '用户是大于10的' : '用户年龄比较小') : '用户需要登录'}"></p>
</div>
</body>
</html>
运行主启动类:在浏览器输入:
点击运算符的使用:
(3)Thymeleaf基本对象-内置对象使用
控制类:
package com.bjpowernode.controller;
import com.bjpowernode.model.SysUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/tpl") //放在类上方的这个注解会和@GetMapping一起组成完整的访问路径
public class ThymeleafController {
//模板内置对象
@GetMapping("/baseObject")
public String baseObject(Model model, HttpServletRequest request, HttpSession session){
model.addAttribute("myname","李思");
request.setAttribute("requestData","request作用域中的数据");
request.getSession().setAttribute("sessionData","session作用域中的数据");
//直接使用session跟request.getSession()作用一样
session.setAttribute("loginname","zhangsan");
return "baseObject";
}
}
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>index.html--学习Thymeleaf语法</h3>
<a href="tpl/expression1">标准变量表达式</a>
<br>
<a href="tpl/expression2">选择变量表达式</a>
<br>
<a href="tpl/link">连接表达式</a>
<br>
<a href="tpl/property">模板属性</a>
<br>
<a href="tpl/eachList">循环list集合</a>
<br>
<a href="tpl/eachArray">循环数组Array</a>
<br>
<a href="tpl/eachMap">循环Map集合</a>
<br>
<a href="tpl/ifunless">判断语句if和unless</a>
<br>
<a href="tpl/switch">判断语句switch</a>
<br>
<a href="tpl/inline">内联iniline</a>
<br>
<a href="tpl/text">字面量</a>
<br>
<a href="tpl/strjoin">字符串连接</a>
<br>
<a href="tpl/sym">运算符的使用</a>
<br>
<a href="tpl/baseObject">内置对象</a>
</body>
</html>
模板:baseObject.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:p="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div style="margin-left: 400px">
<h3>内置对象#request,#session,session的使用</h3>
<p>获取作用域中的数据:</p>
<p th:text="${#request.getAttribute('requestData')}"></p>
<p th:text="${#session.getAttribute('sessionData')}"></p>
<!--#session的简化session-->
<p th:text="${session.loginname}"></p>
</div>
</body>
</html>
运行主启动类:
点击内置对象:
(4)内置对象#request、#session的方法
模板baseObject.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:p="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div style="margin-left: 400px">
<h3>内置对象#request,#session,session的使用</h3>
<p>获取作用域中的数据:</p>
<p th:text="${#request.getAttribute('requestData')}"></p>
<p th:text="${#session.getAttribute('sessionData')}"></p>
<!--#session的简化session-->
<p th:text="${session.loginname}"></p>
<br>
<br>
<h3>使用内置对象的方法</h3>
URL整个完整地址:getRequestURL=<sapn th:text="${#request.getRequestURL()}"></sapn><br>
getRequestURI=<span th:text="${#request.getRequestURI()}"></span><br>
getQueryString=<span th:text="${#request.getQueryString()}"></span><br>
上下文:getContextPath=<span th:text="${#request.getContextPath()}"></span><br>
服务器地址:getServerName=<span th:text="${#request.getServerName()}"></span><br>
端口号:getServerPort=<span th:text="${#request.getServerPort()}"></span><br>
sessionId=<span th:text="${#session.getId()}"></span>
</div>
</body>
</html>
getQueryString:是获取参数:
(5) 一些内置对象事例
ctx:上下文
local:获取环境信息的 就是说你是中文环境啊,那个时区 ,那个地区,本地环境信息的
param:获取参数信息的 ${param.foo}:获取foo这个参数
可以查看网址:
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html