您的位置:首页 > 编程语言 > Java开发

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

2008-03-12 15:01 651 查看
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.

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐