what spring doc mean by all model attributes are considered to be exposed as URI template variables in the redirect URL?

Viewed 41

what spring doc mean by : all model attributes are considered to be exposed as URI template variables in the redirect URL? as i am new to spring if some one help me with example ... thanks advance

what spring doc mean by all model attributes are considered to be exposed as URI template variables in the redirect URL?

1 Answers

That means if you have an attribute in your model, lets say

model.addAttribute("NewAttribute", "WhatsUp");

This attribute newAttribute will be appended to your URL, when you make a redirect. Example: yourUrl.com?newAttribute=Whatsup

If you wanted to pass a variable, but do not want it to show up in your URL as a parameter, then you use addFlashAttribute instead.

Example:

@GetMapping("/redirectWithRedirectAttributes")
public RedirectView redirectWithRedirectAttributes(RedirectAttributes attributes) {
 
    attributes.addFlashAttribute("attributeWhichWillNotShowUpInURL", "redirectWithRedirectAttributes");

    attributes.addAttribute("attributeWhichWillShowUpInURL", "redirectWithRedirectAttributes");

    return new RedirectView("redirectedUrl");
}

According to the documentation, you could also simply choose to request that none of your Model attributes be appended to your URL, on redirect:

requestMappingHandlerAdapter.setIgnoreDefaultModelOnRedirect(true);

The RequestMappingHandlerAdapter provides a flag called "ignoreDefaultModelOnRedirect" that can be used to indicate the content of the default Model should never be used if a controller method redirects

Good Read: https://www.baeldung.com/spring-redirect-and-forward

Documentation: https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-redirect-attributes

Reference: Spring automatic adds model param to url

Related