如何将常用内容包含到多级模板页面中

时间:2013-04-22 21:12:48

标签: jsf facelets

我正在尝试将一个公共页面包含到模板中,但我得到的只是一个没有错误的空白页面。

common.xhtml实际上具有template.xhtml中指示的内容。似乎template.xhtml无法识别两个级别的包含。

的template.xhtml

<html 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:s="http://jboss.com/products/seam/taglib"
 xmlns:c="http://java.sun.com/jstl/core"
 xmlns:ub="http://jboss.com/products/seam/ub-taglib"
 xmlns:rich="http://richfaces.ajax4jsf.org/rich">

<head>
  <ui:insert name="css" />  
  <ui:insert name="header" />
</head>

<body>
 <ui:insert name="body" />  
</body>
</html>

custom.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:s="http://jboss.com/products/seam/taglib"
    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:rich="http://richfaces.ajax4jsf.org/rich"
    xmlns:a4j="https://ajax4jsf.dev.java.net/ajax"
    xmlns:c="http://java.sun.com/jstl/core"
    template="template.xhtml">

    <ui:define name="css">
        <link rel="stylesheet" type="text/css" href="/custom.css/>
    </ui:define>

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

</ui:composition>

common.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:s="http://jboss.com/products/seam/taglib"
    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:rich="http://richfaces.ajax4jsf.org/rich"
    xmlns:a4j="https://ajax4jsf.dev.java.net/ajax"
    xmlns:c="http://java.sun.com/jstl/core"
    template="template.xhtml">


    <ui:define name="header">
        <h1>header</h1>
    </ui:define>
    <ui:define name="body">
        <table><tr><td>Table</td></tr></table>
    </ui:define>    
</ui:composition>

1 个答案:

答案 0 :(得分:2)

这确实行不通。 <ui:define>应该在模板客户端(即包含<ui:composition template="...">的页面)中使用,而不是在包含文件(即<ui:composition>没有template的页面)中使用。但是,您可以从现有的主模板“扩展”。

custom.xhtml

中删除
<ui:include src="common.xhtml" />

common.xhtml

的变化
template="custom.xhtml"

在浏览器中打开common.xhtml而不是custom.xhtml

另见:


无关具体问题,为防止最终用户表单能够直接在浏览器中打开custom.xhtmltemplate.xhtml,建议将其移至{{1文件夹。此外,您是否了解/WEB-INF<h:head>组件?我建议使用它们。此外,让<h:outputStylesheet>最终以<h1>结尾是没有意义的。也许你的意思是<head><ui:insert name="header">里面?此外,您可以轻松地将<body>放在模板中,这样您就不需要在每个模板客户端中重复它们。

<h1>

/WEB-INF/templates/template.xhtml

<html ...> <h:head> </h:head> <h:body> <ui:insert name="header" /> <ui:insert name="body" /> </body> </html> (CSS文件放在/WEB-INF/templates/custom.xhtml文件夹中)

/resources

<ui:composition ... template="/WEB-INF/templates/template.xhtml"> <ui:define name="header"> <h1><ui:insert name="custom-header" /></h1> </ui:define> <ui:define name="body"> <h:outputStylesheet name="custom.css" target="head" /> <ui:insert name="custom-body" /> </ui:define> </ui:composition>

/page.xhtml

另见:

相关问题