复杂参数:
- Map<String, Object> map
- Model model
- HttpServletRequest request
- HttpServletResponse response
以上复杂参数所携带的数据均可被放在 request 请求域中,其中 Map 与 Model 类型处理方法一致。(本文只介绍使用)
使用方法:
1. 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 RequestController { @GetMapping("/params") public String testParam(Map<String, Object> map, Model model, HttpServletRequest request, HttpServletResponse response){ map.put("map", "helloMap"); model.addAttribute("model", "helloModel"); request.setAttribute("message", "helloMessage"); Cookie cookie = new Cookie("c1", "v1"); cookie.setDomain("localhost"); 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("map"); Object model = request.getAttribute("model"); Object message = request.getAttribute("message"); map.put("hello", hello); map.put("medol", model); map.put("message", message); return map; } }
2. 具体解释:
- map、model 里面的数据会被放在request的请求域, 通过request.getAttribute(“数据名”) 取得。
- HttpServletRequest 的数据也会被放在request的请求域, 通过request.getAttribute(“请求名”) 取得。
注意:使用return "forward:/success"转发机制,Controller的注释为 @Controller
3. 执行结果:
通过request取得 Map,Medol,HttpServletRequest 的值如下图所示:
设置cookies成功:
尾注:我是看尚硅谷老师的课学习的SpringBoot,30分钟的课25分钟debug看源码(新手不友好),所以开始时真的很困难,可是只要跑起来就有风不是嘛,哼,死磕到底!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)