1
votes

I would like to get outcome value from navigation rules into a request scoped JSF 2 bean. How can I do that?

For example, when I press on a <h:link outcome="contacts"> and end up in the contacts page, then I would like to get the outcome "contacts" in the backing bean associated with the navigation menu.

faces-config.xml

<navigation-rule>
    ...
    <navigation-case>
        <from-outcome>contacts</from-outcome>
        <to-view-id>/pages/contacts.xhtml</to-view-id>
    </navigation-case>
    ...
</navigation-rule>
1
You have a lot of outcome variables ?Rong Nguyen
Not so. I just wanna get outcome for disable h:link into menuRusfearuth
You need to hard-code in backing bean.Rong Nguyen
I'll do it only as temporary solution. Do you have any ideas?Rusfearuth
You can get current url, and then compare to faces-config's content !Rong Nguyen

1 Answers

5
votes

In JSF, AFAIK, only the ConfigurableNavigationHandler will have that information. So create a custom ConfigurableNavigationHandler that will stash the outcome in a request parameter for your consumption in the destination page.

  1. Your custom navigation handler

    public class NavigationHandlerTest extends ConfigurableNavigationHandler {
    
    private NavigationHandlerTest concreteHandler;
    
       public NavigationHandlerTest(NavigationHandler concreteHandler) {
        this.concreteHandler = concreteHandler;
       }
    
    
    @Override
       public void handleNavigation(FacesContext context, String fromAction, String    outcome){
        //Grab a hold of the request parameter part and save the outcome in it for
        //later retrieval
         FacesContext context = FacesContext.getCurrentInstance();
         ExternalContext ctx = context.getExternalContext();
         ctx.getRequestMap().put("currentOutcome", outcome);
    
        //resume normal navigation
         concreteHandler.handleNavigation(context, fromAction, outcome);   
        }   
      } 
    
  2. Configure your handler in faces-config.xml

      <application>
         <navigation-handler>com.foo.bar.NavigationHandlerTest</navigation-handler>
      </application>
    
  3. Retrieve in your destination bean

      @ManagedProperty(value="#{param.currentOutcome}")
      String outcome;
      //getter and setter