2011-06-29 54 views
1

我无法弄清楚如何在我的Web应用程序的每个页面中包含一段HTML(比如说一点点table)。JSF模板,在每个页面中包含一段HTML

说,这是我想包括表,所以我做了一个模板:

<?xml version ... ?> 
<!DOCTYPE ..."> 
<html xmlns="... all the required namespaces ..."> 
    <head> 
    </head> 
    <body> 
     <table> 
      <tr><td>first</td><td>second</td><td>third</td><td>...</td></tr> 
     </table> 
    </body> 
</html> 

然后我有一个使用它的代码:

<?xml version ...?> 
<!DOCTYPE ..."> 
<html xmlns="... all required namespaces ..."> 

    <body> 
     <h3>Will this be displayed?</h3> 
     <ui:composition template="tableTemplate.xhtml"> 
      <h4>Will this?</h4> 
     </ui:composition> 
    </body> 

</html> 

,我得到的页面浏览器是:

<html xmlns ...> 
    <head> 
    </head> 
    <body> 
     <table> 
      <tr><td>first</td><td>second</td><td>third</td><td>...</td></tr> 
     </table> 
    </body> 
</html> 

所以有表,但其余的都失踪了!

回答

3

TRY

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

,并在您tableTemplate.xhtml

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

    put your template here 

</ui:composition> 
4

在主模板,你需要使用<ui:insert>宣布在模板定义将被插入的地方。

template.xhtml

<!DOCTYPE html> 
<html lang="en" 
    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"> 
    <h:head> 
     <title><ui:insert name="title">Default title</ui:insert></title> 
    </h:head> 
    <h:body> 
     <ui:insert name="body">Default body</ui:insert> 
     <table> 
      <tr><td>first</td><td>second</td><td>third</td><td>...</td></tr> 
     </table> 
    </h:body> 
</html> 

在模板客户端,你需要使用<ui:define>来定义它们是在模板声明的地方要插入的模板定义。

page.xhtml

<ui:composition template="template.xhtml" 
    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="title"> 
     Define your page title here 
    </ui:define> 

    <ui:define name="body"> 
     <h3>Define your body content here</h3> 
     <p>Blah blah</p> 
    </ui:define> 
</ui:composition> 

没有HTML周围<ui:composition>是必要的。无论如何他们将被忽略。只是不要把HTML放在那里,那只会浪费空间并且让你自己感到困惑。

当您在浏览器中打开page.xhtml,下面将在渲染输出结束:

<!DOCTYPE html> 
<html lang="en" 
    xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <title>Define your page title here</title> 
    </head> 
    <body> 
     <h3>Define your body content here</h3> 
     <p>Blah blah</p> 
     <table> 
      <tr><td>first</td><td>second</td><td>third</td><td>...</td></tr> 
     </table> 
    </body> 
</html> 
+0

OK,现在我开始明白这个成分是如何工作的。我看到了这些缺点:1)HTML页面的“常规”结构完全丢失,因为客户端必须定义由模板“粘在一起”的“碎片”2)如果我想插入头部,我有为它提供一个占位符 – AgostinoX

+0

然后就这样做,相反,用''。另请参阅我对上一个问题的回答(以及其中的“另请参阅”链接)。每种方式都有自己的(缺点)优势。我明白“HTML完全丢失”的缺点。但是你也应该理解你不需要用这种方式为每一页重复相同的HTML“线框”的优点。如果您在主布局中进行更改,则必须在每个页面中执行此操作。 – BalusC