jsfnavigationjsf-1.2journey

JSF: How to execute some code on every time a navigation rule is executed?


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).


Solution

  • 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>