【问题标题】:RedirectView vs redirect: in spring mvcRedirectView vs重定向:在spring mvc中
【发布时间】:2015-06-08 09:38:10
【问题描述】:

在spring mvc中使用哪个更好的重定向url:

return new ModelAndView(new RedirectView("../abc/list.vm"));

return new ModelAndView("redirect:DummyRedirectPage.htm");

【问题讨论】:

    标签: spring spring-mvc


    【解决方案1】:

    ModelAndView 对象包含一个引用其视图对象的实例变量,它可以是:

    • 具体的View实现如你使用时的情况:

      new ModelAndView(new RedirectView("../abc/list.vm"))
      
    • 一个 String 对象,包含前缀为 redirect:forward: 的视图符号名称, 使用时就是这样:

      new ModelAndView("redirect:DummyRedirectPage.htm")
      

    现在,当调用 Spring 基础 Web 条目 DispatcherServlet 以呈现 View 时,它将尝试根据给定的 ModelAndView 对象解析请求的视图,或者获取对其 View 子的引用- 实现它提供或解析View 并从ModelAndView 视图字符串表示中创建它:

    protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // ...
        View view;
        if (mv.isReference()) {
            // Resolve the view and instantiate it...
        }
        else {
            // No need to lookup: the ModelAndView object contains the actual View object.
            view = mv.getView();
            // ...
        }
        // ...
    }
    

    请注意,如果视图是 String 对象,org.springframework.web.servlet.ModelAndView#isReference 将返回 true

    鉴于以下细节,我假设第一个 ModelAndView 将涉及较少的计算,因此可能比后者更受欢迎。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 2015-06-05
      • 1970-01-01
      • 1970-01-01
      • 2018-12-29
      相关资源
      最近更新 更多