当前位置: 首页 > 工具软件 > Thymeleaf > 使用案例 >

Thymeleaf 对象、Thymeleaf 内置对象

钱浩荡
2023-12-01

Thymeleaf 对象

Thymeleaf 支持直接访问 Servlet Web 原生资源,HttpServletRequest、HttpServletResponse、
HttpSession、ServletContext。

#request: 获取 HttpServletRequest 对象
#response: 获取 HttpServletResponse 对象
#session: 获取 HttpSession 对象
#servletContext: 获取 ServletContext 对象

```java
@GetMapping("/servlet")
public String servlet(HttpServletRequest request){
 request.setAttribute("value","request");
 request.getSession().setAttribute("value","session");
 request.getServletContext().setAttribute("value","servletContext");
 return "test"; }
<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 支持直接访问 session, ${#request.getAttribute(‘name’)} 也可以简化 ${name}

@GetMapping("/servlet2")
public ModelAndView servlet2(HttpSession session){
 session.setAttribute("name","李四");
 ModelAndView modelAndView = new ModelAndView();
 modelAndView.setViewName("test");
 modelAndView.addObject("name","张三");
 return modelAndView; }
<p th:text="${name}"></p> <p th:text="${#request.getAttribute('name')}"></p> <p th:text="${session.name}"></p> <p th:text="${#session.getAttribute('name')}"></p>

Thymeleaf 内置对象

dates:日期格式化
calendars:日期操作
numbers:数字格式化
strings:字符串格式化
bools:boolean
arrays:数组内置对象
lists:List 集合内置对象
sets:Set 集合内置对象
maps:Map 集合内置对象

@GetMapping("/utility")
public ModelAndView utility(){
 ModelAndView modelAndView = new ModelAndView();
 modelAndView.setViewName("test");
 modelAndView.addObject("date",new Date());
 Calendar calendar = Calendar.getInstance();
 calendar.set(2020,1,1);
 modelAndView.addObject("calendar",calendar);
 modelAndView.addObject("number",0.06);
 modelAndView.addObject("string","Spring Boot");
 modelAndView.addObject("boolean",true);
 modelAndView.addObject("array",Arrays.asList("张三","李四","王五"));
 List<User> list = new ArrayList<>();
 list.add(new User(1,"张三"));
 list.add(new User(2,"李四"));
 modelAndView.addObject("list",list);
 Set<User> set = new HashSet<>();
 set.add(new User(1,"张三"));
 set.add(new User(2,"李四"));
 modelAndView.addObject("set",set);
 Map<Integer,User> map = new HashMap<>();
 map.put(1,new User(1,"张三"));
 map.put(2,new User(2,"李四"));
 modelAndView.addObject("map",map);
 return modelAndView; }
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><br/>
number百分⽐格式化:<span th:text="${#numbers.formatPercent(number,2,2)}"></span> <br/>
name是否为空:<span th:text="${#strings.isEmpty(string)}"></span><br/>
name⻓度:<span th:text="${#strings.length(string)}"></span><br/>
name拼接:<span th:text="${#strings.concat('Good',string)}"></span><br/>
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> <br/>
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>
 类似资料: