【问题标题】:JSF - custom NavigationHandler outcome values invalid?JSF - 自定义 NavigationHandler 结果值无效?
【发布时间】:2012-08-20 21:25:44
【问题描述】:

我为自己写了一个自定义 NavigationHandler 与下面的非常相似,但只是使用堆栈来保存历史记录:

http://jsfatwork.irian.at/book_de/custom_component.html#!idx:/custom_component.html:fig:backnavigationhandler-code

public class HistoryNavigationHandler extends NavigationHandler
{
    private final NavigationHandler navigationHandler;

    private final Stack<String> outcomes;

    public HistoryNavigationHandler(final NavigationHandler navigationHandler)
    {
        this.navigationHandler = navigationHandler;
        this.outcomes = new Stack<String>();
    }

    @Override
    public void handleNavigation(final FacesContext context, final String fromAction, final String outcome)
    {
        if (outcome != null)
        {
            if (outcome.equals("back"))
            {
                final String lastViewId = this.outcomes.pop();

                final ViewHandler viewHandler = context.getApplication().getViewHandler();
                final UIViewRoot viewRoot = viewHandler.createView(context, lastViewId);
                context.setViewRoot(viewRoot);
                context.renderResponse();

                return;
            }
            else
            {
                this.outcomes.push(context.getViewRoot().getViewId());
            }
        }
        this.navigationHandler.handleNavigation(context, fromAction, outcome);
    }
}

在 faces-config.xml 中注册这个:

<navigation-handler>
    package.HistoryNavigationHandler
</navigation-handler>

导致以下日志警告和存在先前工作链接的消息:

Warning: jsf.outcome.target.invalid.navigationhandler.type

Something like: this link is disabled because a navigation case could not be matched

有什么问题?

【问题讨论】:

    标签: jsf jakarta-ee navigation


    【解决方案1】:

    自 JSF 2 起,NavigationHandler 已被 ConfigurableNavigationHandler 取代。所有 JSF 2 特定标签/组件(如 &lt;h:link&gt; 等)都依赖于它。保留NavigationHandler 是为了向后兼容。

    这是一个如何正确扩展 ConfigurableNavigationHandler 的启动示例:

    public class HistoryNavigationHandler extends ConfigurableNavigationHandler {
    
        private NavigationHandler wrapped;
    
        public HistoryNavigationHandler(NavigationHandler wrapped) {
            this.wrapped = wrapped;
        }
    
        @Override
        public void handleNavigation(FacesContext context, String from, String outcome) {
    
            // TODO: Do your job here. 
    
            wrapped.handleNavigation(context, from, outcome);        
        }
    
        @Override
        public NavigationCase getNavigationCase(FacesContext context, String fromAction, String outcome) {
            return (wrapped instanceof ConfigurableNavigationHandler)
                ? ((ConfigurableNavigationHandler) wrapped).getNavigationCase(context, fromAction, outcome)
                : null;
        }
    
        @Override
        public Map<String, Set<NavigationCase>> getNavigationCases() {
            return (wrapped instanceof ConfigurableNavigationHandler)
                ? ((ConfigurableNavigationHandler) wrapped).getNavigationCases()
                : null;
        }
    
    }
    

    【讨论】:

    • 首先非常感谢 :),您在 jsf 方面帮助了我很多。我有几个问题:只要从堆栈中弹出最后一个视图的结果“返回”来自任何操作方法的返回值,它就可以正常工作。但是将其与链接 &lt;h:link outcome="back"/&gt; 一起使用是行不通的。 Navigation case cannot be resolved 将被打印出来。
    • 相应地覆盖 getNavigationCase()getNavigationCases() 以便它返回非 null 的结果 back
    • 感谢您的快速回答,是的,首先考虑了这一点,但我对复杂的 jsf 内容还不太熟悉。这个问题的下一个问题是:我如何将初始请求 url 放入我的导航处理程序堆栈(用户用于进入站点)? (也许我应该提出新的问题)
    • 好问题。我想我会在会话范围 bean 的构造函数中获取它,然后让自定义导航处理程序获取它。
    • 当然是 :) 我至少不会立即看到其他简单的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2017-10-31
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    相关资源
    最近更新 更多