Struts2的 Tiles 配置步骤

ziyifengfei 2011-01-09

Tiles 配置步骤

1. 项目中导入Tiles JAR文件

 (1) tiles-core-version.jar

 (2) tiles-api-version.jar

 (3) tiles-jsp-version.jar

 (4) Jakarta common BeanUtiles

 (5) Jakarta common Logging

 (6) Jakarta common Digester 

 (7) Struts2-tiles-plugin-version.jsp

2. 配置Tiles Listener初始化参数, 用于设置Tiles 组装文件位置和名称

   /WEB-INF/web.xml

    <context-param>

        <param-name>

          org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG

        </param-name>

        <param-value>

          /WEB-INF/tiles-defs.xml,/org/apache/tiles/classpath-defs.xml

        </param-value>

    </context-param>

3.  配置Tiles监听器

<listener>

    <listener-class>org.apache.tiles.web.startup.TilesListener</listener-class>

</listener>

4. 在WEB-INF/下创建Tiles组装配置文件tiles-defs.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE tiles-definitions PUBLIC

       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"

       "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>

<!-- ============================================================================= -->

  <!-- 基础配置 -->

  <definition name=".base" template="/layout/layout.jsp">

      <put-attribute name="top" value="/include/top.jsp" />

      <put-attribute name="left" value="/include/left.jsp" />

      <put-attribute name="bottom" value="/include/bottom.jsp" />

  </definition>

  <definition name=".main_page" extends=".base">

     <put-attribute name="body" value="/main/main.jsp"></put-attribute>

  </definition>

  <definition name=".employee_main_page" extends=".base">

     <put-attribute name="body" value="/employee/main.jsp"></put-attribute>

  </definition>

  <definition name=".employee_add_page" extends=".base">

     <put-attribute name="body" value="/employee/add.jsp"></put-attribute>

  </definition>

<!-- =================================== 产品类别页面配置========================================== -->

<!-- =================================== 产品页面配置========================================== -->

  <definition name=".product_main_page" extends=".base">

     <put-attribute name="body" value="/product/main.jsp"></put-attribute>

  </definition>

  <definition name=".product_add_page" extends=".base">

     <put-attribute name="body" value="/product/add.jsp"></put-attribute>

  </definition>

   <definition name=".product_modify_page" extends=".base">

     <put-attribute name="body" value="/product/modify.jsp"></put-attribute>

  </definition>

   <definition name=".product_delete_page" extends=".base">

     <put-attribute name="body" value="/product/delete.jsp"></put-attribute>

  </definition>

   <definition name=".product_view_page" extends=".base">

     <put-attribute name="body" value="/product/view.jsp"></put-attribute>

  </definition>  

</tiles-definitions>

5. 创建模板文件 /layout/layout.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<table border="1" width="100%">

  <tr>

     <td><tiles:insertAttribute name="top"></tiles:insertAttribute> </td>

  </tr>

</table>

<table border="1" width="100%">

  <tr>

     <td width="20%"><tiles:insertAttribute name="left"></tiles:insertAttribute> </td>

     <td width="80%"><tiles:insertAttribute name="body"></tiles:insertAttribute> </td>

  </tr>

</table>

<table border="1" width="100%">

  <tr>

     <td><tiles:insertAttribute name="bottom"></tiles:insertAttribute> </td>

  </tr>

</table>

</body>

</html>

6. Struts2 中转发到Tiles组装文件的配置

<package name="main_info" namespace="/main" extends="tiles-default" >

    <action name="toMain" class="main_page_action" method="toMain">

        <result name="toMain" type="tiles">.main_page</result>

 </action>

</package>

相关推荐