【问题标题】:Is possible to have in section head a style class and script function defined in included file that is included in another included file可以在节头中包含在包含文件中定义的样式类和脚本函数,该文件包含在另一个包含文件中
【发布时间】:2020-02-14 00:54:29
【问题描述】:

可以在 head /head 部分有样式类和脚本函数,其中样式类和脚本函数在另一个 xhtml 文件中包含的 xhtml 文件中定义。 举个例子:

文件模板.xhtml

<h:body> 

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

</h:body>

文件内容.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>          

文件 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>    

在结果 xhtml 中,我只有一个 javascript 函数,而不是两个 javascript 函数(contentJS 和 subcontentJS),mystyleclass 不在 head 部分。

【问题讨论】:

  • 我有一个不同的问题,因为我有一个包含在另一个文件(名为 content.xhtml)中的包含文件(名为 subcontent.xtml),并且该文件使用一个模板(名为 template.xhtml)。样式类和javascript函数定义在subcontent.xhtml中(不在css文件或js文件中)
  • 你用的是哪个jsf实现和版本?

标签: templates jsf styles head


【解决方案1】:

您的标记中有两个问题:

  1. 定义contentJS 的h:outputScript 块不在&lt;ui:define.../&gt; 块内,因此未合并到呈现的输出中。

  2. style 是一个简单的 html 元素,没有放入 head 中,而是直接渲染。将其更改为 h:outputStylesheet target="head"。

模板.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>

子内容.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>

【讨论】:

  • 感谢 Selaron,但您的代码在 html 结果中不起作用,我只有 subcontentJS 函数,没有 mystyleclass 类。
  • 我使用的是 Mojarra 2.3.3 - 你使用的是哪个实现和版本@Chiuma?
猜你喜欢
  • 2010-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-03
  • 2011-07-17
  • 2014-01-28
  • 1970-01-01
相关资源
最近更新 更多