• Thymeleaf学习(3)—— 内置对象


    一. 基本对象

    1. #request

            #request表示HttpServletRequest对象

    2. #session

            #session表示HttpSession对象

            session是#session的简单表示形式,是map对象,用来获取session域中指定key的值 

    3. #servletContext

            #servletContext表示servletContext对象

            application是#servletContext的简单表示形式,用来获取servletContext域中key的值

    4. param

            param对象表示请求参数的集合

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <div>
    9. 获取request域中的对象 <br/>
    10. <p th:text="${#request.getAttribute('requestName')}">p>
    11. <p th:text="${requestName}">p>
    12. div>
    13. <div>
    14. 获取session域中的对象 <br/>
    15. <p th:text="${#session.getAttribute('sessionName')}">p>
    16. <p th:text="${session.sessionName}">p>
    17. div>
    18. <div>
    19. 获取application域中的对象 <br/>
    20. <p th:text="${#servletContext.getAttribute('applicationName')}">p>
    21. <p th:text="${application.applicationName}">p>
    22. div>
    23. <div>
    24. 获取参数的内容 <br/>
    25. 发送name参数的值:<span th:text="${param.name}">span> <br/>
    26. 参数的个数:<span th:text="${param.size()}">span>
    27. div>
    28. <div>
    29. 内置对象的方法 <br/>
    30. <p th:text="${#request.getRequestURL()}">p>
    31. <p th:text="${#request.getRequestURI()}">p>
    32. <p th:text="${#request.getContextPath()}">p>
    33. <p th:text="${#request.getServerName()}">p>
    34. <p th:text="${#request.getServerPort()}">p>
    35. <p th:text="${#session.getId()}">p>
    36. div>
    37. body>
    38. html>
    1. package com.xdu.studyspringboot.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import javax.servlet.ServletContext;
    5. import javax.servlet.http.HttpServletRequest;
    6. import javax.servlet.http.HttpSession;
    7. @Controller
    8. public class ThymeleafController {
    9. @RequestMapping("/httpObject")
    10. public String testHttpObject(HttpServletRequest request, HttpSession session, String name){
    11. request.setAttribute("requestName", "Tom");
    12. session.setAttribute("sessionName", "Jack");
    13. ServletContext application = request.getServletContext();
    14. application.setAttribute("applicationName", "Mike");
    15. return "test";
    16. }
    17. }

    二. 工具类对象

    1. #dates(日期对象)

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. #dates日期对象 <br/>
    9. <p th:text="${#dates.format(myDate)}">p>
    10. <p th:text="${#dates.format(myDate, 'yyyy-MM-dd')}">p>
    11. <p th:text="${#dates.format(myDate, 'yyyy-MM-dd HH:mm:ss')}">p>
    12. <p th:text="${#dates.year(myDate)}">p>
    13. <p th:text="${#dates.month(myDate)}">p>
    14. <p th:text="${#dates.createNow()}">p>
    15. body>
    16. html>
    1. package com.xdu.studyspringboot.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import javax.servlet.http.HttpServletRequest;
    5. import java.util.Date;
    6. @Controller
    7. public class ThymeleafController {
    8. @RequestMapping("/utilObject")
    9. public String testUtilObject(HttpServletRequest request){
    10. request.setAttribute("myDate", new Date());
    11. return "test";
    12. }
    13. }

    2. #strings

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <p th:text="${#strings.toUpperCase(myStr)}">p>
    9. <p th:text="${#strings.indexOf(myStr,'ll')}">p>
    10. <p th:text="${#strings.substring(myStr, 2, 5)}">p>
    11. <p th:text="${#strings.concat(myStr, 'Java')}">p>
    12. <p th:text="${#strings.length(myStr)}">p>
    13. <p th:text="${#strings.length('happy')}">p>
    14. <p th:text="${#strings.contains(myStr, 'hello')}">p>
    15. body>
    16. html>
    1. package com.xdu.studyspringboot.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import javax.servlet.http.HttpServletRequest;
    5. @Controller
    6. public class ThymeleafController {
    7. @RequestMapping("/utilObject")
    8. public String testUtilObject(HttpServletRequest request){
    9. request.setAttribute("myStr", "hello world");
    10. return "test";
    11. }
    12. }

    3. #lists

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <p th:text="${#lists.size(myList)}">p>
    9. <p th:text="${#lists.contains(myList, 1)}">p>
    10. <p th:text="${#lists.isEmpty(myList)}">p>
    11. <p th:text="${#lists.sort(myList)}">p>
    12. body>
    13. html>
    1. package com.xdu.studyspringboot.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import javax.servlet.http.HttpServletRequest;
    5. import java.util.ArrayList;
    6. import java.util.List;
    7. @Controller
    8. public class ThymeleafController {
    9. @RequestMapping("/utilObject")
    10. public String testUtilObject(HttpServletRequest request){
    11. List list = new ArrayList<>();
    12. list.add(1);
    13. list.add(3);
    14. list.add(2);
    15. request.setAttribute("myList", list);
    16. return "test";
    17. }
    18. }

    4. #numbers

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. #dates日期对象 <br/>
    9. <p th:text="${#numbers.formatCurrency(myNumber)}">p>
    10. <p th:text="${#numbers.formatDecimal(myNumber, 4, 1)}">p>
    11. body>
    12. html>
    1. package com.xdu.studyspringboot.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import javax.servlet.http.HttpServletRequest;
    5. @Controller
    6. public class ThymeleafController {
    7. @RequestMapping("/utilObject")
    8. public String testUtilObject(HttpServletRequest request){
    9. request.setAttribute("myNumber", 12.25);
    10. return "test";
    11. }
    12. }

    5. 处理空值

    通过加问号来判断前面的值是否为空,防止报错 

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <p th:text="${data?.name}">p>
    9. body>
    10. html>
    1. package com.xdu.studyspringboot.controller;
    2. import com.xdu.studyspringboot.pojo.User;
    3. import org.springframework.stereotype.Controller;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import javax.servlet.http.HttpServletRequest;
    6. @Controller
    7. public class ThymeleafController {
    8. @RequestMapping("/utilObject")
    9. public String testUtilObject(HttpServletRequest request){
    10. request.setAttribute("data", new User(1, "Tom", 22));
    11. return "test";
    12. }
    13. }
  • 相关阅读:
    Camera-3A AE/AWB/AF
    SpringBoot 教程及其相关知识整理
    Java#11(字符串练习)
    【CSS3】
    function+bind实现多态(松耦合)
    OSI参考模型个人总结
    设计特性思考
    十八、MySQL添加外键?
    Open3D 点云配准——可视化匹配点对之间的连线
    java+ssm+mysql高校图书管理系统
  • 原文地址:https://blog.csdn.net/Archer__13/article/details/126910047