返回普通模式视图和返回重定向模式视图之间的区别

时间:2014-04-24 08:34:48

标签: java spring-mvc spring-webflow

我刚刚学习了Spring MVC webflow。我对此感到困惑:

return new ModelAndView("test");
return new ModelAndView("redirect:test");

重定向是否就像response.sendRedirect()一样? 我只是检查Spring引用,实际上,return new RedirectView()response.sendRedirect()相同。那么这三者之间有什么区别?

1 个答案:

答案 0 :(得分:1)

如果你想比较ModelAndView(" test")和ModelAndView(" redirect:test") 到servlet框架。第一个类似于RequestDispatcher 而后者将类似于request.sendRedirect() 如果你想知道requestdispatcher和sendredirect之间的区别, 这是that

的答案

即使最终结果是"重定向:测试"是调用HttpServletResponse.sendRedirect(), RedirectView对象为您提供了便利和更多功能 IMO,直接在框架中使用servlet API可能不是一个好主意 你想破解一些东西或解决一些问题。

  

RedirectView发出一个返回的HttpServletResponse.sendRedirect()调用   客户端浏览器作为HTTP重定向。默认情况下,会考虑所有模型属性   在重定向URL中作为URI模板变量公开。剩下的属性   那些是原始类型或原始类型的集合/数组是自动的   附加为查询参数。
  http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/mvc.html

无论如何,在春季MVC中,通常有不止一种方法可以做同样的事情。 这只是编码风格的问题。 例如,

public String doSomething(Model model){
    return "test";
}
public ModelAndView i_am_similar_to_doSomething(){
    return new ModelAndView("test");
}

//You can guess that you can do the same thing with redirect.

还有更多方法可以做这样的事情,但我的建议就是这样 尝试坚持整个应用程序的相同风格。

相关问题