如何在FullAjaxExceptionHandler显示的错误页面中调用p:commandButton?

时间:2013-01-11 06:23:55

标签: jsf primefaces action omnifaces

我正在使用OmniFaces FullAjaxExceptionHandler和PrimeFaces。我在errorPage.xhtml中添加了一个命令按钮,如下所示:

<h:form>
    <p:commandButton id="logoutButtonId" value="Redirect" actionListener="#{userMB.logout()}">
</h:form>

错误页面显示正确,但该按钮不会触发方法#{userMB.logout()}。 这就是我理解在我的设置中发生的事情:

  1. 如果发生错误,FullAjaxExceptionHandler会很好地显示errorPage.xhtml
  2. 如果我点击我添加的按钮,页面会使用相同的数据进行更新,就像刷新一样。
  3. 如果再次单击该按钮,则会触发托管bean方法。
  4. 只有在第二次单击时才会调用bean方法。似乎在第一次加载时,bean方法不会绑定到对象。

    如何在错误页面中添加命令按钮,并在使用FullAjaxExceptionHandler时将action / actionlistener正确绑定到HTML组件?

2 个答案:

答案 0 :(得分:2)

这对我有用,这是BalusC在下面的回答中提到的方法的组合。它适用于IE和Firefox。在某些情况下可能需要进行一些调整,但由于错误页面只有一个表单,所以我没有费心通过表单循环。

var originalPrimeFacesAjaxResponseFunction = PrimeFaces.ajax.AjaxResponse;
PrimeFaces.ajax.AjaxResponse = function(responseXML) {
    var newViewRoot = $(responseXML.documentElement).find("update[id='javax.faces.ViewRoot']").text();

    if (newViewRoot) {
        var viewState = $(responseXML.documentElement).find("update[id='javax.faces.ViewState']").text();

        $('head').html(newViewRoot.substring(newViewRoot.indexOf("<head>") + 6, newViewRoot.indexOf("</head>")));
        $('body').html(newViewRoot.substring(newViewRoot.indexOf("<body>") + 6, newViewRoot.indexOf("</body>")));
        if (!$('input').attr("javax.faces.ViewState")){
            var hidden = document.createElement("input");
            hidden.setAttribute("type", "hidden");
            hidden.setAttribute("name", "javax.faces.ViewState");
            hidden.setAttribute("value", viewState);
            hidden.setAttribute("autocomplete", "off");
            $('form').append(hidden);
        }
    } else {
        originalPrimeFacesAjaxResponseFunction.apply(this, arguments);
    }
};

答案 1 :(得分:0)

此问题有两个可能的原因:

  1. 如果您使用的是Internet Explorer,则可以将其识别为PrimeFaces @all,以便无法在基于IE的浏览器中初始化必要的ajax javascripts。这在这里得到解答:Object doesn't support this property or method with primefaces omnifaces timeout combination

  2. 如果您正在使用其他浏览器并且使用JSF标准<f:ajax>来触发ajax操作,则可以将其识别为JSF自己的jsf.js以无法更新JSF视图状态在@all之后的所有形式。这在这里得到解答:Bug report form on error page

  3. 请注意,这些脚本需要放在调用ajax操作的页面上,而不是放在错误页面本身上。