2010-11-05 61 views
11

我遇到了包括facelet模板的問題。我想分解一些內容,以便我可以在其他地方重新使用它。使用包含在Facelets中的問題

所以我改變了這種代碼:

<!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代碼:組成。

<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的片段。

這是怎麼回事?包含內容以外的內容是否已刪除?我怎樣才能包含頁面,而不需要修剪內容?

回答

11

<ui:define>必須放置在<ui:composition><ui:decorate> a template包含適當的<ui:insert>標籤。您已將其移至<ui:composition>而不是 a template。沒有模板意味着沒有內容。

從技術上講,要達到您的要求,您必須用<ui:insert>替換<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:insert /> 
</ui:composition> 

而且在admin_generic.xhtml宣佈上述頁面(我假設它爲somepage.xhtml)爲template

<!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:include>替換<ui:define>的正文。

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

感謝您的澄清 – 2010-11-08 13:09:40

+0

不客氣。今後儘量減少問題中的不相關噪音,以便其他人儘早回答:) – BalusC 2010-11-08 13:11:00

+0

是的,好的提示。會做 – 2010-11-09 09:39:14

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> 
+2

您不一定需要以root身份聲明該表。一個'ui:作品'會工作得很好。我認爲你的錯誤觀念是由很多審判而引起的。總的來說,'ui:define'必須以'template'的形式出現在'ui:composition'中。 – BalusC 2010-11-08 13:16:24

+0

是的你是對的。您的最後一點是關鍵 – 2010-11-08 13:42:29