When to use NavigationHandler.handleNavigation vs ExternalContext.redirect/dispatch

前端 未结 1 849
心在旅途
心在旅途 2021-02-02 14:36

It would seem that the following are equivalent:

FacesContext.getCurrentInstance().getApplication().getNavigationHandler().handleNavigation(\"/index.xhtml?faces-         


        
相关标签:
1条回答
  • 2021-02-02 15:16

    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.

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

    See also:

    • 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)
    0 讨论(0)
提交回复
热议问题