一. 基本对象
1. #request
#request表示HttpServletRequest对象
2. #session
#session表示HttpSession对象
session是#session的简单表示形式,是map对象,用来获取session域中指定key的值
3. #servletContext
#servletContext表示servletContext对象
application是#servletContext的简单表示形式,用来获取servletContext域中key的值
4. param
param对象表示请求参数的集合
<!DOCTYPE html>
<!--suppress ThymeleafVariablesResolveInspection -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
获取request域中的对象 <br/>
<p th:text="${#request.getAttribute('requestName')}"></p>
<p th:text="${requestName}"></p>
</div>
<div>
获取session域中的对象 <br/>
<p th:text="${#session.getAttribute('sessionName')}"></p>
<p th:text="${session.sessionName}"></p>
</div>
<div>
获取application域中的对象 <br/>
<p th:text="${#servletContext.getAttribute('applicationName')}"></p>
<p th:text="${application.applicationName}"></p>
</div>
<div>
获取参数的内容 <br/>
发送name参数的值:<span th:text="${param.name}"></span> <br/>
参数的个数:<span th:text="${param.size()}"></span>
</div>
<div>
内置对象的方法 <br/>
<p th:text="${#request.getRequestURL()}"></p>
<p th:text="${#request.getRequestURI()}"></p>
<p th:text="${#request.getContextPath()}"></p>
<p th:text="${#request.getServerName()}"></p>
<p th:text="${#request.getServerPort()}"></p>
<p th:text="${#session.getId()}"></p>
</div>
</body>
</html>
package com.xdu.studyspringboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Controller
public class ThymeleafController {
@RequestMapping("/httpObject")
public String testHttpObject(HttpServletRequest request, HttpSession session, String name){
request.setAttribute("requestName", "Tom");
session.setAttribute("sessionName", "Jack");
ServletContext application = request.getServletContext();
application.setAttribute("applicationName", "Mike");
return "test";
}
}
二. 工具类对象
1. #dates(日期对象)
<!DOCTYPE html>
<!--suppress ThymeleafVariablesResolveInspection -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
#dates日期对象 <br/>
<p th:text="${#dates.format(myDate)}"></p> <!--2022年9月18日 GMT+08:00 下午1:40:48-->
<p th:text="${#dates.format(myDate, 'yyyy-MM-dd')}"></p> <!--2022-09-18-->
<p th:text="${#dates.format(myDate, 'yyyy-MM-dd HH:mm:ss')}"></p> <!--2022-09-18 13:40:48-->
<p th:text="${#dates.year(myDate)}"></p> <!--2022-->
<p th:text="${#dates.month(myDate)}"></p> <!--9-->
<p th:text="${#dates.createNow()}"></p> <!--Sun Sep 18 13:40:49 GMT+08:00 2022-->
</body>
</html>
package com.xdu.studyspringboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
@Controller
public class ThymeleafController {
@RequestMapping("/utilObject")
public String testUtilObject(HttpServletRequest request){
request.setAttribute("myDate", new Date());
return "test";
}
}
2. #strings
<!DOCTYPE html>
<!--suppress ThymeleafVariablesResolveInspection -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="${#strings.toUpperCase(myStr)}"></p> <!--HELLO WORLD-->
<p th:text="${#strings.indexOf(myStr,'ll')}"></p> <!--2-->
<p th:text="${#strings.substring(myStr, 2, 5)}"></p> <!--llo-->
<p th:text="${#strings.concat(myStr, 'Java')}"></p> <!--hello worldJava-->
<p th:text="${#strings.length(myStr)}"></p> <!--11-->
<p th:text="${#strings.length('happy')}"></p> <!--5-->
<p th:text="${#strings.contains(myStr, 'hello')}"></p> <!--true-->
</body>
</html>
package com.xdu.studyspringboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class ThymeleafController {
@RequestMapping("/utilObject")
public String testUtilObject(HttpServletRequest request){
request.setAttribute("myStr", "hello world");
return "test";
}
}
3. #lists
<!DOCTYPE html>
<!--suppress ThymeleafVariablesResolveInspection -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="${#lists.size(myList)}"></p> <!--3-->
<p th:text="${#lists.contains(myList, 1)}"></p> <!--true-->
<p th:text="${#lists.isEmpty(myList)}"></p> <!--false-->
<p th:text="${#lists.sort(myList)}"></p> <!--[1, 2, 3]-->
</body>
</html>
package com.xdu.studyspringboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
@Controller
public class ThymeleafController {
@RequestMapping("/utilObject")
public String testUtilObject(HttpServletRequest request){
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(3);
list.add(2);
request.setAttribute("myList", list);
return "test";
}
}
4. #numbers
<!DOCTYPE html>
<!--suppress ThymeleafVariablesResolveInspection -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
#dates日期对象 <br/>
<p th:text="${#numbers.formatCurrency(myNumber)}"></p> <!--¥12.25-->
<p th:text="${#numbers.formatDecimal(myNumber, 4, 1)}"></p> <!--0012.2-->
</body>
</html>
package com.xdu.studyspringboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class ThymeleafController {
@RequestMapping("/utilObject")
public String testUtilObject(HttpServletRequest request){
request.setAttribute("myNumber", 12.25);
return "test";
}
}
5. 处理空值
通过加问号来判断前面的值是否为空,防止报错
<!DOCTYPE html>
<!--suppress ThymeleafVariablesResolveInspection -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="${data?.name}"></p> <!--Tom-->
</body>
</html>
package com.xdu.studyspringboot.controller;
import com.xdu.studyspringboot.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class ThymeleafController {
@RequestMapping("/utilObject")
public String testUtilObject(HttpServletRequest request){
request.setAttribute("data", new User(1, "Tom", 22));
return "test";
}
}