1

Is possible to have a style class and script function in section head /head where the style class and script function is defined in xhtml file that is included in another xhtml file. Here an example:

File template.xhtml

<h:body> 

        <ui:insert name="content" >
         Template content
        </ui:insert>

</h:body>

File content.xhtml

<ui:composition template="template.xhtml">

  <h:outputScript target="head">
        function contentJS()
        {

        }
  </h:outputScript>     

    <ui:define name="content">

        <ui:include src="subcontent.xhtml"/>        

    </ui:define>

</ui:composition>          

File subcontent.xhtml

<ui:composition ...>

  <h:outputScript target="head">
        function subcontentJS()
        {

        }
  </h:outputScript>     

  <style>       
            .mystyleclass {color:red}
  </style>

  <div class="mystyleclass">Text color red</div>

</ui:composition>    

In the result xhtml, i have only one javascript funtion and not the two javascript function (contentJS and subcontentJS) and mystyleclass not is in the head section.

Chiuma
  • 11
  • 2
  • Possible duplicate of [How to customize h:head when using ui:composition template?](https://stackoverflow.com/questions/11545210/how-to-customize-hhead-when-using-uicomposition-template) – Selaron Oct 17 '19 at 07:52
  • I have a differetn problem because i have an included file (named subcontent.xtml) that is included in another file (named content.xhtml) and this file use a template (named template.xhtml). The style class and javascript function are defined in subcontent.xhtml (not in css file or js file) – Chiuma Oct 17 '19 at 08:14
  • Which jsf implementation and version do you use? – Selaron Oct 17 '19 at 09:25

1 Answers1

1

You have two issues in your markup:

  1. The h:outputScript block defining contentJS is not within a <ui:define.../> block and thus not incorporated into the rendered output.

  2. The style is a simple html element not placed into the head but directly rendered. Change this to h:outputStylesheet target="head".

template.xhtml:

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

<h:body>

    <ui:insert name="content">
    Template content
    </ui:insert>

</h:body>
</html>

content.xhtml:

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

<h:body>

    <ui:composition template="template.xhtml">

        <ui:define name="content">

            <h:outputScript target="head">
                function contentJS()
                {

                }
            </h:outputScript>

            <ui:include src="subcontent.xhtml" />

        </ui:define>

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

subcontent.xhtml:

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

<h:body>
    <ui:composition>

        <h:outputScript target="head">
            function subcontentJS()
            {

            }
        </h:outputScript>

        <h:outputStylesheet target="head">
            .mystyleclass {
                color: red
            }
        </h:outputStylesheet>

        <div class="mystyleclass">Text color red</div>

    </ui:composition>
</h:body>
</html>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Selaron
  • 6,105
  • 4
  • 31
  • 39
  • Thanks Selaron but your code not work beacause in the html result, i have only the subcontentJS dunction and not the mystyleclass class. – Chiuma Oct 17 '19 at 10:15
  • I'm using Mojarra 2.3.3 - which implementation and version do you use @Chiuma? – Selaron Oct 17 '19 at 10:18