Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

java - difference between redirect and redirect inside modelandview

In spring controller class to redirect to a url

  • some places all using return "redirect:/abc.htm";.

  • also using return new ModelAndView("redirect:/abc.htm").

Any one please explain the difference and similarities of both statements.

And in which situation it has to use.


Rohit:

Am using RedirectAttribute to get values from old url. In this case am getting value while using this return "redirect:/abc.htm"; but not in this return new ModelAndView("redirect:/abc.htm").
Is there any difference in RedirectAttributes

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The statements:

return "redirect:/abc.htm"
return new ModelAndView("redirect:/abc.htm")

do the same thing: redirects the request to abc.htm. If a view name is returned that has the prefix redirect:, this is recognized as a special indication that a redirect is needed. The rest of the view name will be treated as the redirect URL.

With the statement

return "redirect:/abc.htm"

you can only return the redirect view name.

With ModelAndView you can return both model and view in a single return value:

ModelAndView modelAndView =  new ModelAndView("redirect:/abc.htm");
modelAndView.addObject("modelAttribute" , new ModelAttribute());
return modelAndView;

But the attribute value will not be available in the new redirect request that the client(browser) will make for the URL /abc.htm. The best use of ModelAndView is when you forward the request to a new URL, so that you can return both model and view together in a single return value. For redirect scenarios, if you want to pass attributes, you should use RedirectAttributes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...