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"}
标签:map,hello2,request,hello,添加,import,数据,hello3 From: https://www.cnblogs.com/ixtao/p/16728957.html