何时使用NavigationHandler.handleNavigation与ExternalContext.redirect/dispatch [英] When to use NavigationHandler.handleNavigation vs ExternalContext.redirect/dispatch

查看:126
本文介绍了何时使用NavigationHandler.handleNavigation与ExternalContext.redirect/dispatch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下内容似乎是等效的:

It would seem that the following are equivalent:

FacesContext.getCurrentInstance().getApplication().getNavigationHandler().handleNavigation("/index.xhtml?faces-redirect=true");

FacesContext.getCurrentInstance().getExternalContext().redirect("/testapp/faces/index.xhtml");

有什么区别,什么时候应该使用?

Are there any differences and when should each be used?

推荐答案

使用 NavigationHandler#handleNavigation() 方法取决于您实现的导航处理程序.您或第三方可以在网络应用程序中轻松覆盖/提供此内容.如果您希望进行更细粒度的控制,那么这可能是有利的,但是如果您根本不希望受到外部可控制的影响,那么这可能是不利的.使用某些URL和/或参数可能会导致不同的导航行为.

With the NavigationHandler#handleNavigation() approach you're dependent on the implemented navigation handlers. You or a 3rd party could easily overridde/supply this in the webapp. This can be advantageous if you want more fine grained control, but this can be disadvantagrous if you don't want to have external controllable influences at all. Using certain URLs and/or parameters could potentially result in a different navigation behaviour.

The ExternalContext#redirect() delegates under the covers immediately to HttpServletResponse#sendRedirect(), without involving any navigation handler. So that may be an advantage when using the navigation handler is potentially disadvantageous. But the disadvantage is that it doesn't handle implicit navigation nor takes definied navigation cases into account.

总而言之,这取决于:)如果您只想进行全面的定向重定向,请使用ExternalContext#redirect().如果要按结果而不是URL进行导航,请使用NavigationHandler#handleNavigation().

All in all, it depends :) If you just want a fullworthy and to-the-point redirect, use the ExternalContext#redirect(). If you want to navigate by an outcome instead of an URL, use NavigationHandler#handleNavigation().

  • What is the difference between redirect and navigation/forward and when to use what?
  • How to navigate in JSF? How to make URL reflect current page (and not previous one)

这篇关于何时使用NavigationHandler.handleNavigation与ExternalContext.redirect/dispatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆