如何使用ui:组合模板自定义h:head?

le2*_*omi 12 jsf facelets composition head jsf-2

我正在使用JSF来呈现HTML页面.我像这样设计页面:

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

<h:head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <meta name="language" content="fr" />
    <title><ui:insert name="title">My app</ui:insert></title>
</h:head>

<h:body>
    <div id="top">
        <ui:include src="/header.xhtml"/>
    </div>

    <h:panelGroup id="center" layout="block" >
        <ui:insert name="center"/>
    </h:panelGroup>

    <div id="bottom">
        <ui:include src="/footer.xhtml"/>
    </div>
</h:body>
Run Code Online (Sandbox Code Playgroud)

此模板有一些"客户端"页面,如下所示:

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

<ui:define name="center">
    <ui:define name="title"><h:outputText value="#{myBean.description}"/></ui:define>
    <ui:include src="#{myBean.url}"/>
</ui:define>
Run Code Online (Sandbox Code Playgroud)

在客户端,我必须在标题中添加元信息.如果我们有像outputScript或outputStylesheet这样的标签,可以在文档中的任何地方设置并在html"head"标签中呈现,那就太棒了.

我没有发现任何事情.当我处于这种情况时,有没有办法在标题中添加标记?谢谢 !

Bal*_*usC 21

<h:outputStylesheet>总是自动搬迁到<h:head>,所以你不必担心这一点.因为<h:outputScript>,默认情况下,它与声明它的位置相同,您可以将target属性设置为head,这样它也会自动重新定位到该属性<h:head>.

<ui:define name="center">
    <h:outputStylesheet name="css/style.css" />
    <h:outputScript name="js/script.js" target="head" />
    ...
</ui:define>
Run Code Online (Sandbox Code Playgroud)

对于其他HTML头元信息,出于某种原因,只要有必要,您就可以声明另一个<ui:insert>.

<h:head>
    <ui:insert name="htmlhead" />
</h:head>
Run Code Online (Sandbox Code Playgroud)

你可以使用如下

<ui:define name="htmlhead">
    <meta ... />
</ui:define>
Run Code Online (Sandbox Code Playgroud)

也可以看看: