本文介绍了何时使用 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() 方法您依赖于已实现的导航处理程序.您或第 3 方可以轻松地在 webapp 中覆盖/提供它.如果您想要更细粒度的控制,这可能是有利的,但如果您根本不想有外部可控影响,这可能是不利的.使用某些 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.

ExternalContext#redirect() 在幕后立即代表 HttpServletResponse#sendRedirect(),不涉及任何导航处理程序.因此,当使用导航处理程序可能是不利的时,这可能是一个优势.但缺点是它不处理隐式导航,也不考虑定义的导航情况.

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().

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

07-25 14:59