0

Normally I have my jsf page like this:

  <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:c="http://java.sun.com/jstl/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
template="/WEB-INF/jsf/template.xhtml">

<ui:define name="content">  
    <!-- Content Header -->
    <section class="content-header">

    </section>

    <!-- Main content -->
    <section class="content">
        <h:form>    
            <div>
            <div>
            <div>
                <div>
                    <div >
                            <h:outputText value="Hello World!"/>
                    </div>
                </div>
              </div>
            </div>
          </div>
        </h:form>
    </section>
</ui:define>

As you noticed, I have a template.xhtml which I import on each jsf page. Below is my template.xhtml.

<!DOCTYPE html>
<html 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:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui">

<f:view>
<h:head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <meta http-equiv="Pragma" content="no-cache"/>
    <meta http-equiv="Expires" content="-1"/>
    <h:outputScript library="primefaces" name="jquery/jquery.js" />

    <link type="text/css" rel="stylesheet" href="#{request.contextPath}/css/jsfdefault.css" />

</h:head>
<h:body>
    <div class="outer-container">
        <div class="inner-container">
            <div class="main" style="min-height: 500px">
                <div style="width:100%;border: none;">
                    <ui:include src="/jsf/menu.xhtml"></ui:include>
                </div>
                <div class="content" style="width: 100%; margin-top:-8px;">
                    <ui:insert name="content">
                    </ui:insert>
                </div>
                <div class="clearer"></div>
            </div>
        </div>
    </div>
</h:body>
</f:view>
</html>

Now I want to modify my template.xhtml. I will check if role = Professor, I will call specific template (templateForProfessor.xhtml) and if role = student, I will call another template (templateForStudent.xhtml). I am not sure where to place the if condition. Glad if you can provide a sample.

templateForProfessor.xhtml

  <!DOCTYPE html>
  <html 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:c="http://java.sun.com/jsp/jstl/core"
   xmlns:p="http://primefaces.org/ui">

   <f:view>
   <h:head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
      <meta http-equiv="Pragma" content="no-cache"/>
      <meta http-equiv="Expires" content="-1"/>
      <h:outputScript library="primefaces" name="jquery/jquery.js" />

      <link type="text/css" rel="stylesheet" href="#                  {request.contextPath}/css/jsfdefault.css" />

    </h:head>
    <h:body>
       <div class="outer-container">
        <div class="inner-container">
          <div class="main" style="min-height: 500px">
            <div style="width:100%;border: none;">
                <ui:include src="/jsf/menu.xhtml"></ui:include>
            </div>
            <div class="content" style="width: 100%; margin-top:-8px;">
                <ui:insert name="content">
                </ui:insert>
            </div>
            <div class="clearer"></div>
            </div>
            </div>
           </div>
           </h:body>
         </f:view>
      </html>

templateForStudent.xhtml

   <!DOCTYPE html>
   <html 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:c="http://java.sun.com/jsp/jstl/core"
   xmlns:p="http://primefaces.org/ui">

   <f:view contentType="text/html">
   <h:head>
    <link type="text/css" rel="stylesheet"
        href="#{request.contextPath}/css/default.css" />

   </h:head>


   <h:body>

    <p:layout fullPage="true">

        <p:layoutUnit id="top" position="north" >
            <div>
                <h:form>
                <div>
                    <div style="float: right;">

                    </div>
                </div>
                </h:form>


            </div>
        </p:layoutUnit>


        <p:layoutUnit id="bottom" position="south" >
        </p:layoutUnit>

        <p:layoutUnit id="left" position="west">

            <ui:insert name="leftContent">
            </ui:insert>

        </p:layoutUnit>

        <p:layoutUnit id="right" position="east">

            <ui:insert name="rightContent">
            </ui:insert>

        </p:layoutUnit>

        <p:layoutUnit id="center" position="center" size="0">

        </p:layoutUnit>
    </p:layout>
  </h:body>

 </f:view>
</html>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Alina
  • 369
  • 3
  • 8
  • 18
  • Did you try putting EL in the attributes that references the template? Something like `template="/WEB-INF/jsf/#{myConfigBean.role}.xhtml"` – Kukeltje Jun 19 '16 at 17:10
  • no where to include this? Do i need to remove the body content of template.xhtml and insert my if condition? – Alina Jun 19 '16 at 17:11
  • 1
    No just in the attribute that references the template. An example more in line with what you already have would be `template="/WEB-INF/jsf/templateFor#{myConfigBean.role}.xhtml` in which the `#{myConfigBean.role}` contains either Student of Professor or .... – Kukeltje Jun 19 '16 at 17:15
  • Oh nice idea. I will try this.. – Alina Jun 19 '16 at 17:17
  • 1
    But if the differences are small, you **can** use something like http://stackoverflow.com/questions/26405708/conditionally-including-a-facelet-file-via-uiinclude to where you have one basic template and only parts different – Kukeltje Jun 19 '16 at 17:19
  • Only the content of the will changed.... the tag will remain same and js also. – Alina Jun 19 '16 at 17:21
  • @Kukeltje your EL works but the problem with this solution, I will need to include this el in all page. Likewise the template.xhtml is already there. So i think its better to write the condition in template.xhtml. What say? – Alina Jun 19 '16 at 18:20
  • Search and Replace? It depends on how much is the same like stated above – Kukeltje Jun 19 '16 at 18:35
  • Yes search n replace will do the job. But still I want to know how I can do it in template.xhtml ... – Alina Jun 19 '16 at 18:36

0 Answers0