Interface RedirectAttributes
- All Superinterfaces:
Model
- All Known Implementing Classes:
RedirectAttributesModelMap
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,
attribute values may be formatted as Strings and stored that way to make
them eligible to be appended to the query string or expanded as URI
variables in org.springframework.web.servlet.view.RedirectView.
This interface also provides a way to add flash attributes. For a
general overview of flash attributes see FlashMap. You can use
RedirectAttributes to store flash attributes and they will be
automatically propagated to the "output" FlashMap of the current request.
Example usage in an @Controller:
@RequestMapping(value = "/accounts", method = RequestMethod.POST)
public String handle(Account account, BindingResult result, RedirectAttributes redirectAttrs) {
if (result.hasErrors()) {
return "accounts/new";
}
// Save account ...
redirectAttrs.addAttribute("id", account.getId()).addFlashAttribute("message", "Account created!");
return "redirect:/accounts/{id}";
}
A RedirectAttributes model is empty when the method is called and is never used unless the method returns a redirect view name or a RedirectView.
After the redirect, flash attributes are automatically added to the model of the controller that serves the target URL.
- Since:
- 3.1
- Author:
- Rossen Stoyanchev
-
Method Summary
Modifier and TypeMethodDescriptionaddAllAttributes(Collection<?> attributeValues) Copy all attributes in the suppliedCollectioninto thisMap, using attribute name generation for each element.addAttribute(Object attributeValue) Add the supplied attribute to thisMapusing agenerated name.addAttribute(String attributeName, Object attributeValue) Add the supplied attribute under the supplied name.addFlashAttribute(Object attributeValue) Add the given flash storage using agenerated name.addFlashAttribute(String attributeName, Object attributeValue) Add the given flash attribute.Return the attributes candidate for flash storage or an empty Map.mergeAttributes(Map<String, ?> attributes) Copy all attributes in the suppliedMapinto thisMap, with existing objects of the same name taking precedence (i.e.Methods inherited from interface org.springframework.ui.Model
addAllAttributes, asMap, containsAttribute, getAttribute
-
Method Details
-
addAttribute
Description copied from interface:ModelAdd the supplied attribute under the supplied name.- Specified by:
addAttributein interfaceModel- Parameters:
attributeName- the name of the model attribute (nevernull)attributeValue- the model attribute value (can benull)
-
addAttribute
Description copied from interface:ModelAdd the supplied attribute to thisMapusing agenerated name.Note: Empty
Collectionsare not added to the model when using this method because we cannot correctly determine the true convention name. View code should check fornullrather than for empty collections as is already done by JSTL tags.- Specified by:
addAttributein interfaceModel- Parameters:
attributeValue- the model attribute value (nevernull)
-
addAllAttributes
Description copied from interface:ModelCopy all attributes in the suppliedCollectioninto thisMap, using attribute name generation for each element.- Specified by:
addAllAttributesin interfaceModel- See Also:
-
mergeAttributes
Description copied from interface:ModelCopy all attributes in the suppliedMapinto thisMap, with existing objects of the same name taking precedence (i.e. not getting replaced).- Specified by:
mergeAttributesin interfaceModel
-
addFlashAttribute
Add the given flash attribute.- Parameters:
attributeName- the attribute name; nevernullattributeValue- the attribute value; may benull
-
addFlashAttribute
Add the given flash storage using agenerated name.- Parameters:
attributeValue- the flash attribute value; nevernull
-
getFlashAttributes
Return the attributes candidate for flash storage or an empty Map.
-