Java 重定向和重定向内部模型和视图之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19516719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
difference between redirect and redirect inside modelandview
提问by Monicka Akilan
In spring controller class to redirect to a url
在 spring 控制器类中重定向到一个 url
some places all using
return "redirect:/abc.htm";
.also using
return new ModelAndView("redirect:/abc.htm")
.
一些地方都使用
return "redirect:/abc.htm";
.也使用
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
我正在使用 RedirectAttribute 从旧 url 获取值。在这种情况下,我在使用 this 时获得了价值,return "redirect:/abc.htm";
但在 this 中却没有 return new ModelAndView("redirect:/abc.htm")
。
有什么区别吗RedirectAttributes
采纳答案by Debojit Saikia
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.
做同样的事情:redirects
请求abc.htm
. 如果返回具有前缀的视图名称,则将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
你便可返回model
,并view
在一个单一的返回值:
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
.
但是该属性值在客户端(浏览器)为 URL 发出的新重定向请求中将不可用/abc.htm
。最好的使用ModelAndView
是当你将请求转发到一个新的URL,这样就可以同时返回model
,并view
在一个单一的返回值在一起。对于重定向场景,如果你想传递属性,你应该使用RedirectAttributes
.