关于在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.

 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值