【问题标题】:ExternalContext#dispatch() doesn't workExternalContext#dispatch() 不起作用
【发布时间】:2013-02-27 09:48:49
【问题描述】:

我有服务器端倒数计数器。当它 == 0 时,方法应该执行ExternalContext#dispatch(),但它没有这样做。方法ExternalContext#redirect() 在这个地方可以正常工作。

....
        }else{
        try {
            FacesContext.getCurrentInstance().getExternalContext().dispatch("result.xhtml");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
....

我尝试了几种拼写 url(result,result.xhtml,\result.xhtml 等)的方法,结果相同。

【问题讨论】:

    标签: jsf


    【解决方案1】:

    这不是让 JSF 导航到不同视图的正确方法。

    如果你在一个动作方法中,你应该把它作为字符串返回。

    public String submit() {
        // ...
    
        return "result.xhtml";
    }
    

    或者,如果您不在操作方法中,并且由于某些不清楚的原因无法将其更改为完全有价值的操作方法,请改用NavigationHandler#handleNavigation()

    FacesContext context = FacesContext.getCurrentInstance();
    context.getApplication().getNavigationHandler().handleNavigation(context, null, "result.xhtml");
    

    【讨论】:

    • 感谢您的建议,但在我的情况下它就像ExternalContext#redirect()。我需要“强制”导航,这将模拟按下 <h:commandButton> 或工作操作方法。
    • 不,它根本不执行重定向。它执行前锋。如果您使用result.xhtml?faces-redirect=true 作为结果,它只能像ExternalContext#redirect() 一样工作。
    猜你喜欢
    • 2015-06-24
    • 2016-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 2020-07-27
    相关资源
    最近更新 更多