JSF ajax update CSS

1k views Asked by At

I have a template xhtml :

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

<f:view />
<h:head>

</h:head>

<h:body>
    <f:facet name="last">
        <h:outputStylesheet name="/ressources/css/default.css" />
    </f:facet>

    <!-- Bandeau -->
    <p:layout fullPage="true">
        <p:layoutUnit position="north" style="height: 50px;">
            <h:form id="menuForm">
                <div id="bandeau">
                    <div id="menu" class="bandeauElement">
                        <p:commandLink action="#{myBean.allerVersPremierePage}">
                            <i class="fa fa-arrow-left" style="color: white" />
                            <f:ajax render="content cssContent contentForm" />
                        </p:commandLink>
                    </div>
                </div>
            </h:form>
        </p:layoutUnit>
        <p:layoutUnit position="center">
            <h:form id="contentForm">
                <ui:insert name="content">                                                      
                    <ui:include src="#{facePainter.mainContent}" />                        
                </ui:insert>  
            </h:form>

            <ui:insert name="cssContent"> 
                <h:outputStylesheet library="css" name="#{facePainter.cssContent}"  />                     
            </ui:insert>            

        </p:layoutUnit>
    </p:layout>
</h:body>
</html>

I have a FacePainter :

@ManagedBean(name="facePainter")
@SessionScoped
public class FacePainter {

    private String mainContent = "ressources/fragments/premierePage.xhtml";

    private String cssContent = "ressources/css/default.css";

    //getters/setters

}

and I have an AccueilBean.java with :

public void allerVersDeuxiemePage() {
    facePainter.setMainContent("ressources/fragments/deuxiemePage.xhtml");
    facePainter.setCssContent("ressources/css/deuxiemePage.css");
}

When I click on my commandLink, the secondPage is loading. But if I check my CSS, I don't have "ressources/css/deuxiemePage.css" but href=RES_NOT_FOUND

Do you know why ? Thanks

Edit :

My template

<h:html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

<f:view />
<h:head>
    <f:facet name="first">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="viewport"
            content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0" />
        <meta name="mobile-web-app-capable" content="yes" />
    </f:facet>

    <h:outputScript name="/ressources/js/primeFacesLocales.js" />

    <title>
        <ui:insert name="pageTitle">
            TEST
        </ui:insert>
    </title>

    <link rel="manifest" href="manifest.json" />

    <ui:insert name="dynamicalCss" />
</h:head>


<h:body>

    <h:form id="contentForm">
        <p:growl id="growl" showDetail="true" autoUpdate="true"
            globalOnly="true" />

        <ui:insert name="dynamicalContent">
            <ui:include src="#{facePainter.mainContent}" />
        </ui:insert>
    </h:form>

</h:body>

</h:html>

My ui:composition :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<h:html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui" >

<h:head></h:head>
<h:body>
    <ui:composition>

        <f:facet name="last">
            <h:outputStylesheet name="/ressources/css/default.css" />
            <h:outputStylesheet name="/ressources/css/page1.css" />
        </f:facet>

        <p>Thanks you for the information.</p>  
        <h:commandLink value="Aller vers page2" action="#{myBean.allerVersPage2}" id="btnPage2">               
            <f:ajax render="dynamicalContent dynamicalCss contentForm" />
        </h:commandLink>    
    </ui:composition>
</h:body>
</h:html>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<h:html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui" >

<h:head></h:head>
<h:body>
    <ui:composition>

        <f:facet name="last">
            <h:outputStylesheet name="/ressources/css/default.css" />
            <h:outputStylesheet name="/ressources/css/page2.css" />
        </f:facet>

        <p>Thanks you for the information.</p>  
        <h:commandLink value="Aller vers page1" action="#{myBean.allerVersPage1}" id="btnPage1">               
            <f:ajax render="dynamicalContent dynamicalCss contentForm" />
        </h:commandLink>    
    </ui:composition>
</h:body>
</h:html>

Body is correctly updated but I don't understand why I need to press F5 in order to update CSS. Do you have an idea please :) ? I think this is my last problem before make my CSS :'(

0

There are 0 answers