日期:2014-05-16  浏览次数:20358 次

转载:JSF中使用Tiles框架
from : http://www.iteye.com/topic/220133

共有如下几个步骤:


1) 首先在web.xml文件中配置TilesServlet,并加入一个Tiles Filter的filter类,这个类用于跳转页面。


Xml代码
<
filter>  
  <filter-name>Tiles Filter</filter-name>  
  <filter-class>net.geo.web.TilesFilter</filter-class>  
  <init-param>  
   <param-name>definitions-config</param-name>  
   <param-value>/WEB-INF/classes/META-INF/tiles/tiles-defs.xml</param-value>  
  </init-param>  
 </filter>  
 <filter-mapping>  
  <filter-name>Tiles Filter</filter-name>  
  <url-pattern>*.tiles</url-pattern>  
 </filter-mapping>  
  
<servlet>  
        <servlet-name>Faces Servlet</servlet-name>  
        <servlet-class>  
            org.apache.myfaces.webapp.MyFacesServlet   
        </servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
  
    <servlet-mapping>  
        <servlet-name>Faces Servlet</servlet-name>  
        <url-pattern>*.jsf</url-pattern>  
    </servlet-mapping>  
  
    <servlet>  
        <servlet-name>Tiles Servelt</servlet-name>  
        <servlet-class>  
            org.apache.struts.tiles.TilesServlet   
        </servlet-class>  
        <init-param>  
            <param-name>definitions-config</param-name>  
            <param-value>/WEB-INF/classes/META-INF/tiles/tiles-defs.xml</param-value>  
        </init-param>  
        <init-param>  
            <param-name>definitions-parser-validate</param-name>  
            <param-value>true</param-value>  
        </init-param>  
        <load-on-startup>2</load-on-startup>  
    </servlet>  

注意:这里Tiles Servelt的启动顺序(即load-on-startup)必须在Faces Servlet的后面。



2)配置tiles文件

这里是一个配置实例:

Xml代码
<!DOCTYPE tiles-definitions PUBLIC   
 "-//Apache Software Foundation//DTD Tiles Configuration//EN"   
 "http://jakarta.apache.org/struts/dtds/tiles-config.dtd">  
  
<tiles-definitions>  
    <definition name="mainlayout" page="/tiles_layout/layout.jsp">  
        <put name="header" value="/tiles_layout/header.jsp"></put>  
        <put name="footer" value="/tiles_layout/footer.jsp"></put>  
    </definition>  
  
    <definition name="/jsfpro/account/register" extends="mainlayout">  
        <put name="title" value="to_register" type="string"></put>  
        <put name="content" value="/account/register.jsp"></put>  
    </definition>  
       
    <definition name="/jsfpro/account/userlist" extends="mainlayout">  
        <put name="title" value="userlist" type="string"></put>  
        <put name="content" value="/account/userlist.jsp"></put>  
    </definition>  
</tiles-definitions>  

<!DOCTYPE tiles-definitions PUBLIC
 "-//Apache Software Foundation//DTD Tiles Configuration//EN"
 "http://jakarta.apache.org/struts/dtds/tiles-config.dtd">

<tiles-definitions>
	<definition name="mainlayout" page="/tiles_layout/layout.jsp">
		<put name="header" value="/tiles_layout/header.jsp"></put>
		<put name="footer" value="/tiles_layout/footer.jsp"></put>
	</definition>

	<definition name="/jsfpro/account/register" extends="mainlayout">
		<put name="title" value="to_register" type="string"></put>
		<put name="content" value="/account/register.jsp"></put>
	</definition>
	
	<definition name="/jsfpro/account/userlist" extends="mainlayout">
		<put name="title" value="userlist" type="string">