向request域中添加数据
Map<String,Object> map、Model model、HttpServletRequest request都是可以给request域中放数据,再用request.getAttribute取数据
package com.java.boot.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.Map; @Controller public class DemoController { //都是可以给request域中放数据,再用request.getAttribute取数据 @GetMapping("/params") public String testParam(Map<String,Object> map, Model model, HttpServletRequest request, HttpServletResponse response){ map.put("hello","java"); model.addAttribute("hello2","C++"); request.setAttribute("hello3","python"); Cookie cookie = new Cookie("c1","v1"); response.addCookie(cookie); return "forward:/success";//请求转发到success } @ResponseBody @GetMapping("/success") public Map success(HttpServletRequest request){ Map<String,Object> map = new HashMap<>(); Object hello = request.getAttribute("hello"); Object hello2 = request.getAttribute("hello2"); Object hello3 = request.getAttribute("hello3"); map.put("hello",hello); map.put("hello2",hello2); map.put("hello3",hello3); return map; } }
请求:http://localhost:8080/params
返回:{"hello2":"C++","hello":"java","hello3":"python"}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2020-09-25 controller
2020-09-25 启动程序