简体   繁体   English

何时使用 NavigationHandler.handleNavigation 与 ExternalContext.redirect/dispatch

[英]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()

See also:也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NavigationHandler.handleNavigation()是否清除闪存? - Does NavigationHandler.handleNavigation() clear the flash? 为什么NavigationHandler.handleNavigation()不转发查看ID? - Why is NavigationHandler.handleNavigation() not forwarding to view ID? 如何在JSF2.0中使用ExternalContext.redirect()? - How to use ExternalContext.redirect() in JSF2.0? 临时对话在ExternalContext.redirect()之后没有结束 - Temporary conversation not ending after ExternalContext.redirect() JSF/Mojarra ExternalContext.redirect() 创建一个 HttpSession - JSF/Mojarra ExternalContext.redirect() creates a HttpSession 禁用将页面添加到JSF中ExternalContext.redirect的导航历史记录中 - Disable adding page to navigation history on ExternalContext.redirect in JSF 使用 ExternalContext.redirect() 将面部消息添加到重定向页面 - Adding faces message to redirected page using ExternalContext.redirect() JSF NavigationHandler#handleNavigation + 清除缓存 - JSF NavigationHandler#handleNavigation + clear cache JSF 1.2 NavigationHandler#handleNavigation调用了两次 - JSF 1.2 NavigationHandler#handleNavigation called twice p:commandButton在调用ExternalContext#redirect()时不重定向 - p:commandButton don't redirect when ExternalContext#redirect() is called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM