问题 在Facelets中使用include的问题


我有问题,包括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 tag可用于将另一个Facelets文件包含到您的   文件。它只包含您指定的任何源文件。您可以   包含任何具有的Facelets文件 ui:component 要么 ui:composition 标签    (修改内容以外的内容) 或者只是片段   XHTML或XML。

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


3771
2017-11-05 13:35


起源



答案:


<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)as 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> 根。

<!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>

11
2017-11-08 12:57



谢谢你的澄清 - Shervin Asgari
别客气。将来尽量减少问题中无关的噪音,以便其他人尽快回答:) - BalusC
是的,好的小费。会做 - Shervin Asgari


答案:


<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)as 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> 根。

<!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>

11
2017-11-08 12:57



谢谢你的澄清 - Shervin Asgari
别客气。将来尽量减少问题中无关的噪音,以便其他人尽快回答:) - BalusC
是的,好的小费。会做 - Shervin Asgari


我解决了这个问题 <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>

1
2017-11-08 11:54



您不一定需要将表声明为root。一个 ui:composition 会好起来的。我认为你的误解是由很多试验引起的。整点就是 ui:define 必须进去 ui:composition 用一个 template。 - BalusC
你是对的。你的后一点是关键 - Shervin Asgari