`
xuhc
  • 浏览: 15503 次
  • 性别: Icon_minigender_2
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

关于在SPRING中的页面跳转和重定向(Forward&Redirect)

阅读更多
1,Redirect的两种方式:
   通过在controller的new ModelAndView:
      return new ModelAndView(new RedirectView(this.getViewFilename()));
   通过在controller中使用HttpServletResponse实例:
      response.sendRedirect(this.getViewFilename());
      return null;

   注意:redirect是针对document root的

2,Forward:
   通过在controller的HttpServletRequest实例:
      request.getRequestDispatcher(this.getViewFilename()).forward(request, response);

   注意:forward是针对context root的


ps:Comments:
1. The redirect URL will be shown in browser, while the forward URL won’t.
2. Redirect could go out of the current web context, while forward can’t.
3. Chain actions: Since the web layer is very small, normally there is no need to chain the controllers together because the only functionality is to send out the view. However, in some cases, when we want to avoid duplicated code, we want to chain certain controllers together. For example, certain global pulldown menu shown in every page, or certain breadcrumbs. Right now, this is not supported, and it’s hard to change the code to support this because of the restriction on errors.getModel() (see below). Another way to do this is through interceptors where we can just pass back the models, not the views.
4. In terms of workflow, we might need to chain actions, there is a wizard form controller.
5. include should work in the same way.


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics