Nithin Satheesan
Nithin Satheesan

Reputation: 1726

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

Upvotes: 3

Views: 438

Answers (1)

BalusC
BalusC

Reputation: 1109512

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>

Upvotes: 5

Related Questions