何时使用 NavigationHandler.handleNavigation 与ExternalContext.redirect/dispatch
以下内容似乎是等效的:
FacesContext.getCurrentInstance().getApplication().getNavigationHandler().handleNavigation("/index.xhtml?faces-redirect=true");
FacesContext.getCurrentInstance().getExternalContext().redirect("/testapp/faces/index.xhtml");
是否有任何差异以及何时应使用它们?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
NavigationHandler#handleNavigation()
方法您依赖于实现的导航处理程序。您或第三方可以轻松地在网络应用程序中覆盖/提供此内容。如果您想要更细粒度的控制,这可能是有利的,但如果您根本不想受到外部可控影响,这可能是不利的。使用某些 URL 和/或参数可能会导致不同的导航行为。ExternalContext#redirect()
立即将其委托给HttpServletResponse#sendRedirect()
,不涉及任何导航处理程序。因此,当使用导航处理程序有潜在的缺点时,这可能是一个优点。但缺点是它不处理隐式导航,也不考虑定义的导航情况。总而言之,这取决于:)如果您只想进行全面且切中要害的重定向,请使用
ExternalContext#redirect()
。如果您想通过结果而不是 URL 进行导航,请使用NavigationHandler#handleNavigation()
。另请参阅:
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 toHttpServletResponse#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.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, useNavigationHandler#handleNavigation()
.See also: