YoYo
YoYo

Reputation: 9415

How to test if a JSF navigation case exists during rendering of a page

I am basically having a Template I want to reuse for several of my pages. They all have a look and feel in common where they share a menu that allows them to login and navigate to a Home page. The issue is with the Home page:

  1. When there exists an index (index.xhtml) local to the current page (not the template), then use that as the default. When my template is called from /tools/page (/tools/page.xhtml), then it will point to /tools/index (/tools/index.xthml). With relative path, I can drop the /tools.
  2. Fall back to /index (/index.xhtml).

Example:

<p:menubar>
  <p:menuitem icon="fa fa-home" outcome="/index" value="Home" 
    rendered="#{not outcomeExists('index')}" />
  <p:menuitem icon="fa fa-home" outcome="index" value="Home"
    rendered="#{outcomeExists('index')}" />
  <p:menuitem icon="fa fa-user" value="#{login.name}"/>
</p:menubar>

Of course, I want to translate true JSF Navigation rules for testing if it exists or not, so it will automatically look for index.xhtml, index.jsf, etc.

Upvotes: 1

Views: 208

Answers (1)

YoYo
YoYo

Reputation: 9415

I have been trying - and came to this solution, where have to expose following method on a CDI Bean used within my JSF page:

public boolean outcomeExists(String outcome) {
  FacesContext ctx = FacesContext.getCurrentInstance();
  ConfigurableNavigationHandler configNavHandler = (ConfigurableNavigationHandler)ctx.getApplication().getNavigationHandler();  
  NavigationCase navCase = configNavHandler.getNavigationCase(ctx,null,outcome);
  return navCase != null && navCase.getToViewId(ctx) != null; // the <to-view-id>
}

This was adapted from - Programmatically get navigation case <to-view-id> from faces-config.xml by outcome

Upvotes: 1

Related Questions