Thymeleaf #request和${param}对象的使用
创始人
2024-04-21 13:35:45
0

参考资料

  1. Thymeleaf参考手册(二):标准表达式语法(一)

目录

  • 一. 前期准备
  • 二. controller层
  • 三. 前台HTML
  • 四. 前台内联JS
    • 4.1 #request.getParameterMap()
    • 4.2 #request.getParameter(key)
    • 4.3 ${param}对象
    • 4.4 #request.getContextPath()
    • 4.5 #request.getAttribute(key)
  • 五. 全局变量对象


一. 前期准备

⏹配置文件

server:servlet:context-path: /jmw

⏹form

import lombok.Data;import java.math.BigDecimal;
import java.util.Date;
import java.util.List;@Data
public class Test16Form {private String name;private String sex;private Date birthday;private BigDecimal money;private List tableList;
}

⏹进入页面的url

http://localhost:8080/jmw/test16/init?name=贾飞天&age=18

二. controller层

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;@Controller
@RequestMapping("/test16")
public class Test16Controller {// 注入request对象@Resourceprivate HttpServletRequest request;@GetMapping("/init")public ModelAndView init() {// ⏹获取项目根路径String contextPath = request.getContextPath();System.out.println(contextPath);  // /jmw// ⏹将?key1=value1&key2=value2 转换为 mapMap parameterMap = request.getParameterMap();for (Map.Entry entry : parameterMap.entrySet()) {System.out.println(entry.getKey());System.out.println(Arrays.toString(entry.getValue()));}/*name[贾飞天]age[18]*/// ⏹获取name参数所对应的valueString name = request.getParameter("name");System.out.println(name);  // 贾飞天// 👉将map存入request的Attribute中HashMap map1 = new HashMap<>();map1.put("address", "地球");map1.put("hobby", "睡觉");request.setAttribute("info", map1);// 👉将entity存入request的Attribute中Test16Form test16Form = new Test16Form();test16Form.setName("贾飞天");test16Form.setSex("男");request.setAttribute("entity", test16Form);// 返回到页面中ModelAndView modelAndView = new ModelAndView();modelAndView.setViewName("test16");return modelAndView;}
}

三. 前台HTML

⏹test16.html



Title


当前一共有[[${param.size()}]]个参数
分别为: [[${param.name}]] 和 [[${param.age}]]

👉渲染之后

当前一共有2个参数
分别为: 贾飞天 和 18

四. 前台内联JS

🧐🧐🧐以下所有的JS代码必须包裹在内联标签中

4.1 #request.getParameterMap()

⏹获取url传参的所有参数

const paramMap = [[${#request.getParameterMap()}]];
console.log(paramMap);
/*{"name": ["贾飞天"],"age": ["18"]}
*/

4.2 #request.getParameter(key)

⏹获取指定的参数

const paramName = [[${#request.getParameter('name')}]];
console.log(paramName);  // 贾飞天

4.3 ${param}对象

⏹param是一个由Thymeleaf 提供的对象,用于存放url传参的所有参数
相当于#request.getParameterMap(),可以理解为简写形式

const param = [[${param}]];
console.log(param);
/*{"name": ["贾飞天"],"age": ["18"]}
*/
// 👉相当于 #request.getParameter('name')
const name = [[${param.name}]];
console.log(name);  // 贾飞天// 👉由于没有hobby,所以渲染完之后为 null,又因为 ?? 表达式,所以最后结果为 吃饭
const hobby = [[${param.hobby}]] ?? '吃饭';
console.log(hobby);  // 吃饭

4.4 #request.getContextPath()

⏹获取设置的项目根路径

const contextPath1 = [[${#request.getContextPath()}]];
console.log(contextPath1);  // /jmw// 👉#httpServletRequest和#request等效
const contextPath2 = [[${#httpServletRequest.getContextPath()}]];
console.log(contextPath2);  // /jmw

4.5 #request.getAttribute(key)

⏹获取请求时,设置的Attribute值

const info = [[${#request.getAttribute('info')}]];
console.log(info);
// {address: "地球", hobby: "睡觉"}const entity = [[${#request.getAttribute('entity')}]];
console.log(entity);
// {name: "贾飞天", sex: "男", birthday: null, money: null, tableList: null}

五. 全局变量对象

😵之前的做法,需要将变量先存入页面的隐藏域中,然后使用js获取隐藏域的值



🧐现在可以这么做,直接将后台的数据封装为一个全局参数对象


相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...