JSF Language switcher and ajax update

2020-04-11 10:34发布

问题:

This question is a follow - up of the older Language switcher implementation in JSF 2.

The substance of the question is still valid, although PrimeFaces has released some newer builds, and my JSF knowledge is a bit better than before.

To summarize, I have a single xhtml page, fully ajaxified.

The language switcher is this:

<h:form id="selectLanguage" >
    <p:commandLink action="#{languageSwitcher.setLanguage('it')}" rendered="#{languageSwitcher.language!='it'}" >
        <h:graphicImage library ="images" name="it.gif" title="it" />
    </p:commandLink>
    <p:commandLink action="#{languageSwitcher.setLanguage('en')}" rendered="#{languageSwitcher.language!='en'}" >
        <h:graphicImage library ="images" name="en.gif" title="en" />
    </p:commandLink>
</h:form>

I want that when it is chosen, only the en flag is displayed, and viceversa. And I want that all the stuff in the web page is updated with the Resource Bundle translations.

In particular, I have some p:dialogS whose header attributes need to be updated as well. The dialog forms are inside each dialog:

<p:dialog header="#{msgs.myHeader}"  ... >
    <h:form .... />
</p:dialog >

The rest of the page is a <p:layout> containing some layoutUnits. Each layoutUnit contains a form with other components that need to be translated as well.

LanguageSwitcher is a SessionScoped JSF Managed Bean.

I have tried all of the following ways:

  1. <f:ajax render="@all" /> inside the p:commandLink braces
  2. update="@all" attribute inside p:commandLink
  3. <p:ajax update="@all" /> inside the p:commandLink braces

Unfortunately, none of them works.

I have tried with @all, although I am available to insert the ids of the forms, they are not so many. Problem is that the dialog headers are not updated in this way.

I am using PrimeFaces 3.4.1 - Mojarra 2.1.13

回答1:

The update="@all" of all PrimeFaces versions until now (3.4.2) is known to fail in IE. Any JavaScript code delivered along with the ajax response isn't been initialized properly.

This is discussed in this PrimeFaces forum topic and reported as issue 4731.

Until they fix it, your best bet is to workaround it by loading the following piece of JavaScript on every view which (possibly) contains an update="@all" command:

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

    if (newViewRoot) {
       $('head').html(newViewRoot.substring(newViewRoot.indexOf("<head>") + 6, newViewRoot.indexOf("</head>")));
       $('body').html(newViewRoot.substring(newViewRoot.indexOf("<body>") + 6, newViewRoot.indexOf("</body>")));
    } else {
        originalPrimeFacesAjaxResponseFunction.apply(this, arguments);
    }
};

Provide this in flavor of a JS file which is loaded by <h:outputScript target="head"> inside the <h:body> in order to enforce the right loading order.

<h:body>
    <h:outputScript name="script.js" target="head" />
    ...
</h:body>