Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke p:commandButton in the error page shown by FullAjaxExceptionHandler?

I am using OmniFaces FullAjaxExceptionHandler and PrimeFaces. I added a command button in the errorPage.xhtml as follows:

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

The error page gets displayed properly, but the button doesn't fire the method #{userMB.logout()}. This is what I understand what happens in my setup:

  1. If an error occurs, FullAjaxExceptionHandler nicely displays errorPage.xhtml.
  2. If I click on the button I added, the page gets updated with the same data, like as if a a refresh happens.
  3. If I click on the button again, then the managed bean method is triggered.

It is only in the second click that the bean method gets called. Seems that on first load, the bean method doesn't gets bound to the object.

How do I implement adding a command button in an error page with an action/actionlistener being properly bound to an HTML component when using FullAjaxExceptionHandler?

like image 628
mutya Avatar asked Mar 21 '26 11:03

mutya


1 Answers

This was what worked for me, a combination of the approach mentioned by BalusC in his answer below. It works in both IE and Firefox. It might need some tweaking for some cases but since the error page I got only has one form, I didn't bother to loop thru the forms.

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);
    }
};
like image 168
mutya Avatar answered Mar 25 '26 00:03

mutya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!