2016-11-10 39 views
1

我有一個ui:composition位於模板中,我想重複模板和組合本身中的元素。如何使用ui:define和ui:insert在相同的ui:組合中?

下面是一個例子。

的template.xhtml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:c="http://java.sun.com/jsp/jstl/core" 
     xmlns:fn="http://java.sun.com/jsp/jstl/functions" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"> 

    <h:head> 
     <title>JSF 2.0 Hello World</title> 
     <link rel="stylesheet" type="text/css" href="css/style.css"/>    
    </h:head> 

    <h:body>  
     <div class = "header"> 
      <div class = "mylogo"></div>    
      HEADER 
      <!-- I want this content logo to be defined within module.xhtml-->  
      <ui:insert name = "content-logo"></ui:insert>    
     </div> 

     <div class = "content"> 
      <ui:insert name = "content"></ui:insert> 
     </div> 

     <div class ="footer"> 
      FOOTER 
     </div>    
    </h:body> 
</html> 

module.xhtml

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


       <!-- define the module content-logo here--> 
       <ui:define name ="content-logo"> 
        <div class ="another-logo"> foo</div> 
       </ui:define> 

       <ui:define name ="content">     

        <-- we also want to display the content-logo here--> 
        <ui:insert name = "content-logo"/> 
        <div class = "foo"> 
         My main content 
        </div> 

       </ui:define> 

</ui:composition> 

所定義的內容的標識將顯示模板頭部細內 - 但它不會在組合物中顯示身體。有沒有辦法可以使這項工作無需複製HTML?

+0

像這樣:http://stackoverflow.com/questions/4792862/how-to-include-another-xhtml-in- XHTML-使用的JSF-2-0-小面 – Kukeltje

回答

0

您可以使用用戶界面:在您的module.xhtml這樣

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


       <!-- define the module content-logo here--> 
       <ui:define name ="content-logo"> 
        <ui:include src="logo.xhtml"/> 
       </ui:define> 

       <ui:define name ="content">     

        <-- we also want to display the content-logo here--> 
        <ui:include src="logo.xhtml"/> 
        <div class = "foo"> 
         My main content 
        </div> 

       </ui:define> 

</ui:composition> 
相關問題