2

I have read the following topic:

Spring RedirectAttributes: addAttribute vs addFlashAttribute

As I understand it, the main difference between a flash attribute and a plain attribute is that a plain attribute can save only Strings and primitives.

But in practice I see another difference:

I have the following controller method:

....
redirectAttributes.addAttribute("error", messageSource.getMessage("AdminController.negativeSum", null, Locale.forLanguageTag("ru-RU")));
return "redirect:/admin/confirmWithdrawRequest";

and

@RequestMapping(value = "admin/confirmWithdrawRequest", method = RequestMethod.GET)
public String confirmWithdrawRequestShowPage(Model model) {
    Set<Event> cashOutEvents = eventJournalService.getEventsByType(EventType.CASHOUT_REQUEST);
    model.addAttribute("events", cashOutEvents);
    model.addAttribute("statuses", EventStatus.values());
    return "admin/confirmWithdrawRequest";
}

on the page confirmWithdrawRequest.jsp

I wrote the following code fragment:

<c:choose>
    <c:when test="${not empty error}">
        <div class="errorblock">
               ${error}
        </div>
    </c:when>
</c:choose>

Now I don't see the error message on my page.

If I replace redirectAttributes.addAttribute with redirectAttributes.addFlashAttribute - the error message will be shown.

Please explain this behaviour and show a scenario when I should use the addAttribute method.

Community
  • 1
  • 1
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • downwoter, please explain why? – gstackoverflow Nov 21 '15 at 11:27
  • Your understanding is not correct, `addAttribute` and `addFlashAttribute` are for different purposes. You should read these two sections of the docs: [Passing Data To the Redirect Target](https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-redirecting-passing-data) and [Using flash attributes](https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-flash-attributes). – Roman Nov 21 '15 at 12:11
  • @Roman does redirect attributes just passes to url and doesn't add to model? – gstackoverflow Nov 21 '15 at 12:18
  • [`RedirectAttributes`](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/support/RedirectAttributes.html) _is_ a `Model` (but it is used only in case of a redirect), so I don't really understand the question. – Roman Nov 21 '15 at 12:25
  • I gave detailed descriprion in topic I believe – gstackoverflow Nov 21 '15 at 12:45

1 Answers1

2

As Spring Documentation states:

By default all model attributes are considered to be exposed as URI template variables in the redirect URL. Of the remaining attributes those that are primitive types or collections/arrays of primitive types are automatically appended as query parameters.

So if you are using addAttribute it just adds the parameter onto your URI. In your case after the first method's invocation there will be a redirect to the URI: YOUR_HOST/admin/confirmWithdrawRequest?error=Sometext. After the redirect your confirmWithdrawRequestShowPage will be executed, and the error parameter will bind, but as you are not storing it it won't be available to the JSP.

In the case of addFlashAttribute, the attribute is not added to the URI but is stored in the session (within the server) and they are available until the first read after the redirect.

So to summarise you can think of addAttribute as something which is sent to the client and then you are getting it back with the URI, and addFlashAttributes as something which is stored at the server only.

If you want your attribute which you have added via addAttribute to be available after the redirect you can just change a bit of your code as follows:

@RequestMapping(value = "admin/confirmWithdrawRequest", method = RequestMethod.GET)
public String confirmWithdrawRequestShowPage(@RequestParam(value = "error") String error, Model model) {
    Set<Event> cashOutEvents = eventJournalService.getEventsByType(EventType.CASHOUT_REQUEST);
    model.addAttribute("events", cashOutEvents);
    model.addAttribute("statuses", EventStatus.values());
    model.addAttribute("error", error);
    return "admin/confirmWithdrawRequest";
}
Mike Muske
  • 323
  • 1
  • 16
Babl
  • 7,446
  • 26
  • 37