1

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?

javaway
  • 127
  • 2
  • 8

1 Answers1

0

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

JCompetence
  • 6,997
  • 3
  • 19
  • 26
  • but this not the case in : Person p=new Person("name", "age"); model.addAttribute("per", p); – javaway Dec 02 '21 at 07:50
  • Maybe clarify @javaway Is the person json not appended to your url? or? – JCompetence Dec 02 '21 at 07:57
  • person not appended to the url – javaway Dec 02 '21 at 08:51
  • Kindly update your question with your code. Expected results and what results you are getting now – JCompetence Dec 02 '21 at 10:01
  • model.addAttribute("NewAttribute", "WhatsUp"); NewAttribute will be appended to the URL but Person p=new Person("name", "age"); model.addAttribute(" person", "p"); p not appended to the URL – javaway Dec 02 '21 at 15:26
  • yes, it will be appended to the URL (as part of the URL String you see), If you want to know how to work with Plain Java Objects, in SpringMVC, please look at `@ModelAttribute`, otherwise, this may also give you hints https://dzone.com/articles/using-spring-mvc%E2%80%99s – JCompetence Dec 02 '21 at 15:28
  • Full Code @PostMapping("/regist") public String regist(Person person, Model model) { Person p = new Person("name", "age"); model.addAttribute("per", p); return "redirect:/info"; } @GetMapping("/info") public String info() { return "result"; } per not appended to the URL – javaway Dec 02 '21 at 18:36