如何从Java类访问faces-config的导航用例



我遇到了这种情况,我必须从托管bean访问整个faces配置。更具体地说,我需要访问faces配置中指定的导航案例列表,并在其中循环。有办法得到它们吗?

我看到NavigationCase有一些很好的方法可以揭示一些有用的信息。。现在的问题是,如何获得这些NavigationCase 的列表

根据您在问题中指定的标签,我可以说您正在使用JSF 2,因此您可以使用ConfigurableNavigationHandler来获得您想要的内容。

使用ConfigurableNavigationHandler#getNavigationCases()获得导航案例的Map,您可以从它的Javadocs:中获得有关该方法的更多信息

返回密钥所在的Map<String, Set<NavigationCase>>值,其中设置值集合中的每个元素都是适用于该元素的NavigationCase.

以下是调用该方法的示例:

FacesContext context = FacesContext.getCurrentInstance();
ConfigurableNavigationHandler navigationHandler = (ConfigurableNavigationHandler) context.getApplication().getNavigationHandler();
Map<String,Set<NavigationCase>> navigationCases = navigationHandler.getNavigationCases();

如果您已经知道要导航到的页面的名称,您可以简单地使用该示例(假设您的页面文件是next.xhtml):

FacesContext context = FacesContext.getCurrentInstance();
ConfigurableNavigationHandler navigationHandler= (ConfigurableNavigationHandler) context.getApplication().getNavigationHandler();
navigationHandler.performNavigation("next");

另请参阅:

  • JSF 2和Post/Rerective/Get
  • javax.faces.application.ConfigurableNavigationHandle的Java代码示例

试试这个代码:

FacesContext ctxt = FacesContext.getCurrentInstance();
ConfigurableNavigationHandler configNavHandler = (ConfigurableNavigationHandler)ctxt.getApplication().getNavigationHandler();
NavigationCase navCase = configNavHandler.getNavigationCase(ctxt,null,"Page");
String toViewId = navCase.getToViewId(ctxt);

相关内容

最新更新