在Facelets中使用include的问题

时间:2010-11-05 13:35:43

标签: jsf include facelets

我遇到问题,包括facelet模板。我想拆分一些内容,以便我可以在其他地方重用它。

所以我改变了这段代码:

<!DOCTYPE html>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    template="/layout/template.xhtml">

    <ui:define name="head">
        <title>Title</title>
    </ui:define>

    <ui:define name="header">
        <h3>Header</h3>
    </ui:define>

    <ui:define name="content">
        <table><tr><td>table</td></tr></table>
    </ui:define>
</ui:composition>

对此:

<!DOCTYPE html>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    template="/layout/template.xhtml">

    <ui:define name="head">
        <title>Title</title>
    </ui:define>

    <ui:include src="/admin/admin_generic.xhtml"/>
</ui:composition>

admin-generic.xhtml里面,我将代码包装在一个ui:composition中。

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

    <ui:define name="header">
        <h3>Header</h3>
    </ui:define>

    <ui:define name="content">
        <table><tr><td>table</td></tr></table>
    </ui:define>
</ui:composition>

但没有显示任何内容。我只是得到一个空白页面,没有错误。使用ui:composition是错误的吗?我试过ui:component,但这也没有帮助。


更新:根据我的Facelets要点指南,它说:

  

ui:include标记可用于将另一个Facelets文件包含到您的   文献。它只包含您指定的任何源文件。您可以   包含具有ui:componentui:composition标记的任何Facelets文件   (修改内容以外的内容)或仅仅是片段   XHTML或XML。

这是怎么回事?包括在内的内容是否已被删除?如何在不修剪内容的情况下包含页面?

2 个答案:

答案 0 :(得分:11)

<ui:define>必须放在<ui:composition><ui:decorate> 一个template,其中包含相应的<ui:insert>标记。您已将其移至<ui:composition> ,而不是 template。没有模板意味着没有内容。

从技术上讲,要达到您的要求,您必须将<ui:include>替换为<ui:insert>

<!DOCTYPE html>
<ui:composition
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    template="template.xhtml">

    <ui:define name="head">
        <title>Title</title>
    </ui:define>

    <ui:insert />
</ui:composition>

并在somepage.xhtml中将上一页(我假设为template)声明为admin_generic.xhtml

<!DOCTYPE html>
<ui:composition
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    template="somepage.xhtml">

    <ui:define name="header">
        <h1>Header</h1>
    </ui:define>

    <ui:define name="content">
        <table><tr><td>table</td></tr></table>
    </ui:define>
</ui:composition>

请注意,您必须在浏览器中打开admin_generic.xhtml。如果您打算在浏览器中打开somepage.xhtml,则<ui:define>必须留在somepage.xhtml。但是,您可以用简单的<ui:define>替换<ui:include>的正文。

<!DOCTYPE html>
<ui:composition 
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    template="template.xhtml">

    <ui:define name="head">
        <title>Title</title>
    </ui:define>

    <ui:define name="header">
        <h1>Header</h1>
    </ui:define>

    <ui:define name="content">
        <ui:include src="admin_generic.xhtml" />
    </ui:define>
</ui:composition>

它允许<ui:composition>,因此您不一定需要将<table>置于root。

<!DOCTYPE html>
<ui:composition 
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">

    <table><tr><td>table</td></tr></table>
</ui:composition>

答案 1 :(得分:1)

我通过移除<ui:composition><ui:define>并直接在<table>添加命名空间来解决这个问题:

<table class="adminform" xmlns="http://www.w3.org/1999/xhtml"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a="http://richfaces.org/a4j">

所以现在我的页面看起来像这样:

<ui:define name="content">
    <ui:include src="/admin/admin_generic.xhtml" />
</ui:define>
相关问题