Maff
Maff

Reputation: 1032

Spring MVC Redirect Attribute Messages

I am having some issues while displaying messages after I have successfully, or unsuccessfully performed some type of CRUD operation (CREATE, DELETE, etc). I have attempted to use Redirect Flash Attributes, although I have found no luck with these and I cannot get the message displaying at all. For example I have declared something like this within my Controller method:

public String DeleteAction(Model model, Object object, @RequestParam int id, RedirectAttributes attributes) {
   // Method logic
   object.delete(id);
   attributes.addFlashAttribute("success", "Object has been removed successfully.");
   return "index"; // View resolver redirect
}

That is an example of my function within one of my controllers where I declare the flash attribute to be binded to the view. I call the flash attribute like this within the .jsp ${success}, although I still cannot get it to display. Is there anything I am missing which is not enabling this to work?

Upvotes: 4

Views: 6658

Answers (1)

Ankur Singhal
Ankur Singhal

Reputation: 26067

A specialization of the Model interface that controllers can use to select attributes for a redirect scenario. Since the intent of adding redirect attributes is very explicit -- i.e. to be used for a redirect URL.

@RequestMapping(value = "/delete", method = RequestMethod.GET)
public String DeleteAction(Model model, Object object, @RequestParam int id RedirectAttributes attributes) {
    object.delete(id);
    attributes.addFlashAttribute("success", "Object has been removed successfully.");
    return "redirect:" + "index";
}

Upvotes: 5

Related Questions