在modelandview内重定向和重定向之间的区别 [英] difference between redirect and redirect inside modelandview
问题描述
在spring控制器类中重定向到一个url
In spring controller class to redirect to a url
-
一些地方都使用
returnredirect :/abc.htm;
。
还使用返回新的ModelAndView(redirect:/ abc .htm)
。
任何人请解释两种陈述的区别和相似之处。
Any one please explain the difference and similarities of both statements.
在哪种情况下必须使用。
And in which situation it has to use.
Rohit:
使用RedirectAttribute从旧网址获取值。
在这种情况下,在使用此返回redirect:/abc.htm;
时获取价值但不在此返回新的ModelAndView(redirect:/abc.htm)
。
RedirectAttributes
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
推荐答案
陈述:
return "redirect:/abc.htm"
return new ModelAndView("redirect:/abc.htm")
做同样的事情:重定向
请求 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.
使用语句
return "redirect:/abc.htm"
您只能返回重定向查看名称。
you can only return the redirect view name.
使用 ModelAndView
,您可以返回模型
和在单个返回值中查看
:
ModelAndView modelAndView = new ModelAndView("redirect:/abc.htm");
modelAndView.addObject("modelAttribute" , new ModelAttribute());
return modelAndView;
但是客户端(浏览器)将要生成的新重定向请求中的属性值不可用对于URL /abc.htm
。最好使用 ModelAndView
是将请求转发到新URL,以便您可以返回模型
和在一个返回值中一起查看
。对于重定向方案,如果要传递属性,则应使用 RedirectAttributes
。
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
.
这篇关于在modelandview内重定向和重定向之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!