JSF:如何在每次执行导航规则时执行一些代码?

Nit*_*san 3 navigation jsf jsf-1.2 journey

有什么办法可以在执行 a 时执行一些代码navigation rule。基本上我想要做的是拥有一个JourneyTrackerbean 来保存旅程的当前状态(比如用户所在的当前页面)。

Bal*_*usC 5

您可以NavigationHandler为此使用自定义。基本上,只需按如下方式扩展该抽象类:

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

}
Run Code Online (Sandbox Code Playgroud)

要让它运行,只需在以下位置注册它faces-config.xml

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

}
Run Code Online (Sandbox Code Playgroud)