向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"}

 

posted @   iTao0128  阅读(161)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2020-09-25 controller
2020-09-25 启动程序
点击右上角即可分享
微信分享提示