【问题标题】:JSF: How to execute some code on every time a navigation rule is executed?JSF:如何在每次执行导航规则时执行一些代码?
【发布时间】:2012-08-23 23:08:39
【问题描述】:

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

【问题讨论】:

    标签: jsf navigation jsf-1.2 journey


    【解决方案1】:

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

    【讨论】:

    • @BaluC 太棒了!有效。有没有办法找出下一个视图ID? handleNavigation 只给出 fromAction 和结果。
    • 不幸的是,你不能。但是,您可以使用fromActionoutcome 手动/以编程方式执行NavigationHandler 的导航,如FacesContext.getCurrentInstance().getApplication().getNavigationHandler() 获得的那样。
    • 是否有@Annotation 无需编辑即可使用faces-config.xml
    • P.S.为什么使用这个处理程序会导致像service jboss.deployment.unit."project-web-1.0.war".component."org.jboss.weld.servlet.WeldInitialListener".START (no longer required) 这样的消息,并且一些链接附加了像This link is disabled because a navigation case could not be matched. 这样的消息?
    • @T.G 提示:问题是关于 JSF 1.x。您需要寻找 JSF 2.x 目标问题和答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多