#request表示HttpServletRequest对象
#session表示HttpSession对象
session是#session的简单表示形式,是map对象,用来获取session域中指定key的值
#servletContext表示servletContext对象
application是#servletContext的简单表示形式,用来获取servletContext域中key的值
param对象表示请求参数的集合
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- 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";
- }
- }
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- #dates日期对象 <br/>
- <p th:text="${#dates.format(myDate)}">p>
- <p th:text="${#dates.format(myDate, 'yyyy-MM-dd')}">p>
- <p th:text="${#dates.format(myDate, 'yyyy-MM-dd HH:mm:ss')}">p>
-
- <p th:text="${#dates.year(myDate)}">p>
- <p th:text="${#dates.month(myDate)}">p>
-
- <p th:text="${#dates.createNow()}">p>
-
- 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";
- }
- }
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
-
- <p th:text="${#strings.toUpperCase(myStr)}">p>
- <p th:text="${#strings.indexOf(myStr,'ll')}">p>
- <p th:text="${#strings.substring(myStr, 2, 5)}">p>
- <p th:text="${#strings.concat(myStr, 'Java')}">p>
- <p th:text="${#strings.length(myStr)}">p>
- <p th:text="${#strings.length('happy')}">p>
- <p th:text="${#strings.contains(myStr, 'hello')}">p>
-
- 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";
- }
- }
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
-
- <p th:text="${#lists.size(myList)}">p>
- <p th:text="${#lists.contains(myList, 1)}">p>
- <p th:text="${#lists.isEmpty(myList)}">p>
- <p th:text="${#lists.sort(myList)}">p>
-
- 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
list = new ArrayList<>(); - list.add(1);
- list.add(3);
- list.add(2);
-
- request.setAttribute("myList", list);
- return "test";
- }
- }
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- #dates日期对象 <br/>
- <p th:text="${#numbers.formatCurrency(myNumber)}">p>
- <p th:text="${#numbers.formatDecimal(myNumber, 4, 1)}">p>
- 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";
- }
- }
通过加问号来判断前面的值是否为空,防止报错
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <p th:text="${data?.name}">p>
- 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";
- }
- }