版權聲明:本文爲 小異常 原創文章,非商用自由轉載-保持署名-註明出處,謝謝!
本文網址:https://blog.csdn.net/sun8112133/article/details/106961086
文章目錄
本篇博客主要講解 Thymeleaf 訪問 Servlet Web 對象以及各種內置對象的使用。
一、Thymeleaf 對象
Thymeleaf 支持直接訪問 Servlet Web 對象,也就是 HttpServletRequest、HttpServletResponse、HttpSession、ServletContext 對象。
#request
:獲取 HttpServletRequest 對象;#response
:獲取 HttpServletResponse 對象;#session
:獲取 HttpSession 對象;#servletContext
:獲取 ServletContext 對象。
1、Thymeleaf
<!-- 直接獲取 request、response、session、servletContext 對象 -->
<p th:text="${#request.getAttribute('value')}"></p>
<p th:text="${#session.getAttribute('value')}"></p>
<p th:text="${#servletContext.getAttribute('value')}"></p>
<p th:text="${#response}"></p>
<!-- Thymeleaf 提供了簡易方法,可以直接訪問 request 和 session 對象 -->
<p th:text="${value}"></p>
<p th:text="${session.value}"></p>
2、Controller
@GetMapping("/servlet")
public String servlet(HttpServletRequest request) {
request.setAttribute("value", "request");
request.getSession().setAttribute("value", "session");
request.getServletContext().setAttribute("value", "servletContext");
return "result";
}
3、瀏覽器測試
源代碼:
二、Thymeleaf 內置對象
Thymeleaf 提供了一組內置對象,使用起來與 Java 中一些工具類用法類似。
- dates: 日期格式化內置對象,參照
java.util.Date
的使用; - calendars: 日期操作內置對象,參照
java.util.Calendar
的使用; - numbers: 數字格式化內置對象;
- strings: 字符串格式化內置對象,參照
java.lang.String
的使用; - bools: boolean 類型內置對象;
- arrays: 數組操作內置對象,參照
java.utils.Arrays
的使用; - lists: List 集合內置對象,參照
java.util.List
的使用; - sets: Set 集合內置對象,參照
java.util.Set
的使用; - maps: Map 集合內置對象,參照
java.util.Map
的使用。
1、dates 與 calendars 對象(日期格式與日期操作)
-
Thymeleaf
date格式化:<span th:text="${#dates.format(date,'yyyy-MM-dd')}"></span><br> 當前日期:<span th:text="${#dates.createToday()}"></span><br> 當前時間:<span th:text="${#dates.createNow()}"></span><br> calendar格式化:<span th:text="${#calendars.format(calendar,'yyyy-MM-dd')}"></span>
-
Controller
@GetMapping("/result21") public ModelAndView result21() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("result"); modelAndView.addObject("date", new Date()); Calendar calendar = Calendar.getInstance(); calendar.set(2019, 1, 1); // 月份爲 0 開始,所以此時是 2019年2月1日 modelAndView.addObject("calendar", calendar); return modelAndView; }
-
瀏覽器測試
-
源代碼
2、numbers 與 strings 對象(數字格式與字符串格式)
-
Thymeleaf
number百分比格式化:<span th:text="${#numbers.formatPercent(number,2,2)}"></span><br> string是否爲空:<span th:text="${#strings.isEmpty(string)}"></span><br> string長度:<span th:text="${#strings.length(string)}"></span><br> string拼接:<span th:text="${#strings.concat('Hello World',string)}"></span>
-
Controller
@GetMapping("/result22") public ModelAndView result22() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("result"); modelAndView.addObject("number", 0.06); modelAndView.addObject("string", "Spring Boot"); return modelAndView; }
-
瀏覽器測試
-
源代碼
3、bools 與 arrays 對象(布爾操作與數組操作)
-
Thymeleaf
boolean是否爲true:<span th:text="${#bools.isTrue(boolean)}"></span><br> arrays的長度:<span th:text="${#arrays.length(array)}"></span><br> arrays是否包含張三:<span th:text="${#arrays.contains(array,'張三')}"></span>
-
Controller
@GetMapping("/result23") public ModelAndView result23() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("result"); modelAndView.addObject("boolean", true); modelAndView.addObject("array", Arrays.asList("張三", "李四", "王五")); return modelAndView; }
-
瀏覽器測試
-
源代碼
4、lists、sets 與 maps 對象(集合操作)
-
Thymeleaf
list是否爲空:<span th:text="${#lists.isEmpty(list)}"></span><br> list長度:<span th:text="${#lists.size(list)}"></span><br> set是否爲空:<span th:text="${#sets.isEmpty(set)}"></span><br> set長度:<span th:text="${#sets.size(set)}"></span><br> map是否爲空:<span th:text="${#maps.isEmpty(map)}"></span><br> map長度:<span th:text="${#maps.size(map)}"></span>
-
Controller
@GetMapping("/result24") public ModelAndView result24() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("result"); List<User> list = new ArrayList<>(); list.add(new User(1L, "張三", 1)); list.add(new User(2L, "李四", 0)); list.add(new User(3L, "王五", 1)); modelAndView.addObject("list", list); Set<User> set = new HashSet<>(); set.add(new User(1L, "張三", 1)); set.add(new User(2L, "李四", 0)); set.add(new User(3L, "王五", 1)); modelAndView.addObject("set", set); Map<Long, User> map = new HashMap<>(); map.put(1L,new User(1L, "張三", 1)); map.put(2L,new User(2L, "李四", 0)); map.put(3L,new User(3L, "王五", 1)); modelAndView.addObject("map", map); return modelAndView; }
-
瀏覽器測試
-
源代碼