springboot-データを運ぶためのリダイレクトRedirectAttributes
コントローラレイヤーが指定されたページにリダイレクトする必要があるときにデータを運ぶ方法は?
- セッションの従来の使用
- RedirectAttributesを使用します(セッションの原則を使用)
- 利点:addFlashAttributeなどのメソッドを提供します。データを一度だけ使用してから削除できるようにします。
RedirectAttributesの使用
public interface RedirectAttributes extends Model {
RedirectAttributes addAttribute(String var1, @Nullable Object var2);
RedirectAttributes addAttribute(Object var1);
RedirectAttributes addAllAttributes(Collection<?> var1);
RedirectAttributes mergeAttributes(Map<String, ?> var1);
RedirectAttributes addFlashAttribute(String var1, @Nullable Object var2);
RedirectAttributes addFlashAttribute(Object var1);
Map<String, ?> getFlashAttributes();
}
-
コントローラのパラメータに直接RedirectAttributesを追加します。
-
addFlashAttributeは、このデータをフェッチするために次のページにリダイレクトした後、セッション内のデータを削除します。\
-
addFlashAttributeメソッドはセッションにデータを保存し、1回の訪問後に無効になります
@PostMapping("/regist")
public String register(RedirectAttributes attribdatautes){
int data = 1;
attributes.addFlashAttribute("data",data);
return "redirect:http://auth.gulimail.com/reg.html";
}
- addAttributeメソッドは、URLの後にデータを接続します(getの形式で)
@GetMapping("/addToCartSuccess.html")
public String addToCartSuccessPagez(@RequestParam("skuId") Long skuId,Model model){
CartItem cartItem = cartService.selectCartItemInfo(skuId);
model.addAttribute("item",cartItem);
return "success";
}