記事ディレクトリ
知識のポイントが少ない
マップ、モデル(マップとモデルのデータは、リクエストのリクエストフィールド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()を使用して設定できます。