SpringBoot:マップとモデルはどのように処理されますか?Springboot Issue 9

知識のポイントが少ない

マップ、モデル(マップとモデルのデータは、リクエストのリクエストフィールドrequest.setAttributeに配置されます)、Errors / BindingResult、RedirectAttributes(リダイレクトはデータを運ぶ)、ServletResponse(レスポンス)、SessionStatus、UriComponentsBuilder、ServletUriComponentsBuilder

Map <String、Object>マップ、モデルモデル、HttpServletRequestリクエストはすべて、リクエストフィールドにデータを入力できます。

 @GetMapping("/params")
    public String testParam(Map<String,Object> map, Model model, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
    
    
        map.put("hello","thisworld");
        model.addAttribute("model","this is model");
        Cookie cookie=new Cookie("c1","v1");
        cookie.setDomain("localhost");
        httpServletResponse.addCookie(cookie);


        return "forward:/success";
    }

マップとモデルのデータは
ここに画像の説明を挿入
、リクエストフィールドデータを取得するためにリクエストリクエストフィールドに保存されます

@ResponseBody
    @GetMapping("/success")
    public Map success(@RequestAttribute(value = "msg",required = false) String msg,
                       @RequestAttribute(value = "code",required = false)Integer code,
                       HttpServletRequest request){
    
    
        Object msg1 = request.getAttribute("msg");

        Map<String,Object> map = new HashMap<>();
        Object hello = request.getAttribute("hello");
        Object world = request.getAttribute("world");
        Object message = request.getAttribute("message");

        map.put("reqMethod_msg",msg1);
        map.put("annotation_msg",msg);
        map.put("hello",hello);
        map.put("world",world);
        map.put("message",message);

        return map;
    }

ここに画像の説明を挿入
ブラウザのCookie設定も有効になります
ここに画像の説明を挿入

マップとモデルのデータがリクエストリクエストフィールドに保存される理由

MapとModelのアドレスが同じであることがわかります。つまり
ここに画像の説明を挿入
、それらは同じオブジェクト
ここに画像の説明を挿入
であり、すべてがその中にあり
ここに画像の説明を挿入
ます。このオブジェクトが何であるかを見てみましょう。
ここに画像の説明を挿入

マップとモデルに設定したものをリクエストフィールドに入れるにはどうすればよいですか?

ソースコードは非常に簡単にトラバースし、request.setAttribute()を使用して設定できます。
ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/qq_47431361/article/details/123750365