版权声明:本文为 小异常 原创文章,非商用自由转载-保持署名-注明出处,谢谢!
本文网址: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; }
-
浏览器测试
-
源代码