问题
I have done template in 3 steps:
Firstly i created a xhtml file with content that is common to all the pages:
AllComponentsTemplate.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<head>
<title>State Transport Department- Work Schedule</title>
<meta name="viewport" content="width=device-width"/>
<link rel="stylesheet" type="text/css" href="../../CSS/CompleteTemplateCSS.css"/>
<link rel="stylesheet" type="text/css" href="../../CSS/templateCSS.css"/>
</head>
<body>
<div class="container">
<div class="header"></div>
<div class="menu">
<h:outputLabel class="welcomeNote" value="#{welcomeBean.fullname}" />
<ul class="ulForMyAccount"><li> <h:outputLabel value="My Account" id="mainAnchor"/>
<ul ><li> <a href="/webpages/ChangePasswordxhtml.xhtml" id="subAnchor" >Change my Password</a> </li> </ul></li></ul> </div>
<div class="contentBody">
<div class="menuTable">
<table class="templateBody" >
<tr>
<td class="navigationLink" > <ul><li>
<h:link value="Home" outcome="/webpages/NewWelcome.xhtml" rendered="#{welcomeBean.home}" class="mainLinks"/>
</li></ul> </td>
</tr> </table> </div> </div>
<div class="footer"></div>
</div> </body> </html>
Then i created a MainTemplate.xhtml page which i would be using in other pages as templates:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<f:view>
<h:head>
<title>State Transport Department-Work Schedule</title>
</h:head>
<h:body>
<ui:include src="AllComponentsTemplate.xhtml"/>
<ui:insert name="informationBody"></ui:insert>
</h:body> </f:view> </html>
Then i added this template to my first page Trial.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/webpages/templates/CompleteTemplate.xhtml">
<ui:define name="informationBody">
<h:form>
<h:commandButton id="allocateButton" value="Test Submit" action="#{empDutySchedBean.testMethod}"/>
</h:form>
</ui:define>
</ui:composition>
Note: trial.xhtml doesn't have a html, head, body tag.
Problem: till the MainTemplate.xhtml page, all the components including the link etc were perfectly visible. However after including the template into Trial.xhtml page, The My Account, Welcome label, Home naviagation link didn't appear.
Apart from this, all the css i applied for the webpage's background color appeared.
I dont know why. Help would be dearly appreciated. I am a beginner at coding.
Thank You! :)
回答1:
Define your AllComponentsTemplate.xhtml as ui:composition as well it doesn't need html, head and body, and as AllComponentsTemplate.xhtml will be included, it doesn't need to add template attribute, or define sections.
If i were you I would set AllComponentsTemplate.xhtml as the main template it self with html, h:head and h:body, etc.., and let Trial.xhtml implement it.
Later on if you needed another pages to have a shared view, you go ahead and create AnotherComponentsTemplate.xhtml and let them implement it.
回答2:
Refer this link it will help you as you are begineer.
http://www.mkyong.com/jsf2/jsf-2-templating-with-facelets-example/
and considering your problem use h:head and h:body for all html tags as Najjar metioned in his answer.
来源:https://stackoverflow.com/questions/23008471/template-jsf-is-not-working-properly-when-i-override-the-elements-they-dont