模板中的JSF參數(shù)

2018-02-22 16:01 更新

JSF教程 - 模板示例中的JSF參數(shù)


以下代碼顯示了如何在模板中使用參數(shù)。

例子

以下代碼來(lái)自commonFooter.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:ui="http://java.sun.com/jsf/facelets"
      >
    <body>
      <ui:composition>
  
      <h1>This is default footer</h1>
        
      </ui:composition>
    </body>
</html>

下面的代碼來(lái)自UserBean.java。

package cn.w3cschool.common;


import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
 
@ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable{
  
  private static final long serialVersionUID = 1L;
  
}

以下代碼來(lái)自demo.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:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      >
    <h:body>
      
      <ui:composition template="template/common/commonLayout.xhtml">

      <ui:param name="curFileName" value="default.xhtml"></ui:param>
      
      </ui:composition>
      
    </h:body>

</html>

以下代碼來(lái)自commonHeader.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:ui="http://java.sun.com/jsf/facelets"
      >
    <body>
      <ui:composition>
  
      <h1>This is default header</h1>
      <h2>Tag Line : #{tagLine}</h2>
      
      </ui:composition>
    </body>
</html>


以下代碼來(lái)自common-style.css。

#page{
  width: 800px;
  margin: 0 auto;
}

#header{
  width: 100%;
  height:150px;
  border: 1px solid #000;
  margin-bottom:16px;
}

#content{
  width: 100%;
  height:200px;
  margin-right:16px;
  margin-bottom:16px;
  border: 1px solid #000;
}

#footer{
  width: 100%;
  height:100px;
  border: 1px solid #000;
}

以下代碼來(lái)自commonContent.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:ui="http://java.sun.com/jsf/facelets"
      >
    <body>
      <ui:composition>
  
      <h1>This is default content</h1>
        
      </ui:composition>
    </body>
</html>


以下代碼來(lái)自commonLayout.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:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      >

  <h:head>
    <h:outputStylesheet name="common-style.css" library="css" />
  </h:head>
    
    <h:body>

  <div id="page">
    
    <div id="header">
      <ui:insert name="header" >
        <ui:include src="/template/common/commonHeader.xhtml">
          <ui:param name="tagLine" value="value from param"></ui:param>
        </ui:include>
      </ui:insert>
      </div>
      
      <div id="content">
        Current File : #{curFileName}
         <ui:insert name="content" >
           <ui:include src="/template/common/commonContent.xhtml" />
         </ui:insert>
      </div>
        
      <div id="footer">
        <ui:insert name="footer" >
          <ui:include src="/template/common/commonFooter.xhtml" />
        </ui:insert>
      </div>
      
    </div>
    
    </h:body>

</html>

以下代碼來(lái)自page1.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:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      >
    <h:body>
      
      <ui:composition template="/template/common/commonLayout.xhtml">
      
        
        <ui:define name="header">
          <h2>This is page1 content</h2>
        </ui:define>
      
      </ui:composition>
      
    </h:body>

</html>
下載 Parameter-In-Template.zip

運(yùn)行

將生成的WAR文件從目標(biāo)文件夾復(fù)制到Tomcat部署文件夾,并運(yùn)行Tomcat-Install-folder/bin/startup.bat。

Tomcat完成啟動(dòng)后,在瀏覽器地址欄中鍵入以下URL。

http://localhost:8080/simple-webapp/demo.xhtml
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)