[英]When to use NavigationHandler.handleNavigation vs 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?有什么区别吗?什么时候应该使用它们?
With the NavigationHandler#handleNavigation()
approach you're dependent on the implemented navigation handlers.使用
NavigationHandler#handleNavigation()
方法,您依赖于已实现的导航处理程序。 You or a 3rd party could easily overridde/supply this in the webapp.您或第 3 方可以轻松地在 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.
使用某些 URL 和/或参数可能会导致不同的导航行为。
The ExternalContext#redirect()
delegates under the covers immediately to HttpServletResponse#sendRedirect()
, without involving any navigation handler. ExternalContext#redirect()
在幕后立即委托给HttpServletResponse#sendRedirect()
,而不涉及任何导航处理程序。 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.
但缺点是它不处理隐式导航,也不考虑定义的导航情况。
All in all, it depends:) If you just want a fullworthy and to-the-point redirect, use the ExternalContext#redirect()
.总而言之,这取决于:) 如果您只想要一个完全有价值的直接重定向,请使用
ExternalContext#redirect()
。 If you want to navigate by an outcome instead of an URL, use NavigationHandler#handleNavigation()
.如果您想通过结果而不是 URL 进行导航,请使用
NavigationHandler#handleNavigation()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.