h:commandButton/h:commandLink does not work on first click, works only on second click

前端 未结 2 1726
北海茫月
北海茫月 2020-11-22 05:23

We have an ajax navigation menu which updates a dynamic include. The include files have each their own forms.


    

        
2条回答
  •  爱一瞬间的悲伤
    2020-11-22 05:38

    You can use the following script to fix the Mojarra 2.0/2.1/2.2 bug (note: this doesn't manifest in MyFaces). This script will create the javax.faces.ViewState hidden field for forms which did not retrieve any view state after ajax update.

    jsf.ajax.addOnEvent(function(data) {
        if (data.status == "success") {
            fixViewState(data.responseXML);
        }
    });
    
    function fixViewState(responseXML) {
        var viewState = getViewState(responseXML);
    
        if (viewState) {
            for (var i = 0; i < document.forms.length; i++) {
                var form = document.forms[i];
    
                if (form.method == "post") {
                    if (!hasViewState(form)) {
                        createViewState(form, viewState);
                    }
                }
                else { // PrimeFaces also adds them to GET forms!
                    removeViewState(form);
                }
            }
        }
    }
    
    function getViewState(responseXML) {
        var updates = responseXML.getElementsByTagName("update");
    
        for (var i = 0; i < updates.length; i++) {
            var update = updates[i];
    
            if (update.getAttribute("id").match(/^([\w]+:)?javax\.faces\.ViewState(:[0-9]+)?$/)) {
                return update.textContent || update.innerText;
            }
        }
    
        return null;
    }
    
    function hasViewState(form) {
        for (var i = 0; i < form.elements.length; i++) {
            if (form.elements[i].name == "javax.faces.ViewState") {
                return true;
            }
        }
    
        return false;
    }
    
    function createViewState(form, viewState) {
        var hidden;
    
        try {
            hidden = document.createElement(""); // IE6-8.
        } catch(e) {
            hidden = document.createElement("input");
            hidden.setAttribute("name", "javax.faces.ViewState");
        }
    
        hidden.setAttribute("type", "hidden");
        hidden.setAttribute("value", viewState);
        hidden.setAttribute("autocomplete", "off");
        form.appendChild(hidden);
    }
    
    function removeViewState(form) {
        for (var i = 0; i < form.elements.length; i++) {
            var element = form.elements[i];
            if (element.name == "javax.faces.ViewState") {
                element.parentNode.removeChild(element);
            }
        }
    }
    

    Just include it as inside the of the error page. If you can't guarantee that the page in question uses JSF , which would trigger auto-inclusion of jsf.js, then you might want to add an additional if (typeof jsf !== 'undefined') check before jsf.ajax.addOnEvent() call, or to explicitly include it by

    
    

    Note that jsf.ajax.addOnEvent only covers standard JSF and not e.g. PrimeFaces or as they use under the covers jQuery for the job. To cover PrimeFaces ajax requests as well, add the following:

    $(document).ajaxComplete(function(event, xhr, options) {
        if (typeof xhr.responseXML != 'undefined') { // It's undefined when plain $.ajax(), $.get(), etc is used instead of PrimeFaces ajax.
            fixViewState(xhr.responseXML);
        }
    }
    

    Update if you're using JSF utility library OmniFaces, it's good to know that the above has since 1.7 become part of OmniFaces. It's just a matter of declaring the following script in the . See also the showcase.

    
        
        ...
    
    

提交回复
热议问题