Spring RedirectAttributes: addAttribute() vs addFlashAttribute()

前端 未结 3 1542
故里飘歌
故里飘歌 2020-11-28 03:00

My understanding so far is on our controller request mapping method we can specify RedirectAttributes parameter and populate it with attributes for when the request

3条回答
  •  北海茫月
    2020-11-28 03:35

    Here is the difference:

    • addFlashAttribute() actually stores the attributes in a flashmap (which is internally maintained in the users session and removed once the next redirected request gets fulfilled)

    • addAttribute() essentially constructs request parameters out of your attributes and redirects to the desired page with the request parameters.

    So the advantage of addFlashAttribute() will be that you can store pretty much any object in your flash attribute (as it is not serialized into request params at all, but maintained as an object), whereas with addAttribute() since the object that you add gets transformed to a normal request param, you are pretty limited to the object types like String or primitives.

提交回复
热议问题