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

时间:2012-08-29 10:50:33

标签: jsf navigation jsf-1.2 journey

每当执行navigation rule时,我是否可以执行某些代码。基本上我要做的是拥有一个JourneyTracker bean,它将保持当前的旅程状态(就像用户所在的当前页面一样)。

1 个答案:

答案 0 :(得分:4)

您可以使用自定义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.
    }

}

要让它运行,只需在faces-config.xml

中注册如下
<application>
    <navigation-handler>com.example.MyNavigationHandler</navigation-handler>
</application>