3

Is there any way I can execute some code whenever a navigation rule is executed. Basically what I am trying to do is to have a JourneyTracker bean which will hold the current state of the journey (like the current page the user is in).

Nithin Satheesan
  • 1,546
  • 3
  • 17
  • 30

1 Answers1

5

You can use a custom NavigationHandler for this. Basically, just extend that abstract class as follows:

public class MyNavigationHandler extends NavigationHandler {

    private NavigationHandler wrapped;

    public MyNavigationHandler(NavigationHandler wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public void handleNavigation(FacesContext context, String from, String outcome) {
        // Do your job here. Just check if "from" and/or "outcome" matches the desired rule.

        wrapped.handleNavigation(context, from, outcome); // Important! This will perform the actual navigation.
    }

}

To get it to run, just register it as follows in faces-config.xml:

<application>
    <navigation-handler>com.example.MyNavigationHandler</navigation-handler>
</application>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @BaluC Awesome! It worked. Is there any way to figure out the next view-id? handleNavigation just gives fromAction and outcome. – Nithin Satheesan Aug 30 '12 at 09:43
  • Unfortunately, you can't. You can however use `fromAction` and `outcome` to manually/programmatically perform a navigation by `NavigationHandler` as obtained by `FacesContext.getCurrentInstance().getApplication().getNavigationHandler()`. – BalusC Aug 30 '12 at 10:55
  • Is there a `@Annotation` for using it without editing `faces-config.xml`? – T.G Apr 30 '14 at 11:55
  • P.S. Why using this handler causing messages like `service jboss.deployment.unit."project-web-1.0.war".component."org.jboss.weld.servlet.WeldInitialListener".START (no longer required)` and some links have attached messages like `This link is disabled because a navigation case could not be matched.`? – T.G Apr 30 '14 at 12:33
  • @T.G Hint: question is about JSF 1.x. You need to look for a JSF 2.x targeted question&answer. – BalusC Apr 30 '14 at 12:37