javaocean

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            3 隨筆 :: 3 文章 :: 0 評論 :: 0 Trackbacks

          開發環境:JDK6,MyEclipse8.0GA, Tomcat6.0。
          頁面設計:

          1.新建web工程tilesdemo。右鍵點擊項目名稱>MyEclipse>增加Strusts2支持,選中Struts2核心庫和Tiles插件庫。 

          2.在web.xml中配置監聽器加載Tiles插件。

          3. 根據設計在/WEB-INF/common/下新建四個JSP頁面。

          header.jsp 代表頁頭,
          body.jsp代表頁面主體,
          footer.jsp代表頁腳部分,
          simpleTpl.jsp是根據前面的設計將頁面標題和上面幾個頁面按上、中、下組裝起來的簡單模版頁面。

          4. 在WEB-INF下新建tiles.xml,編寫一個簡單的模版simpleTpl, 同時編寫一個首頁繼承simpleTpl,不同之處僅僅是標題。
          tiles.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="simpleTpl" template="/WEB-INF/jsp/common/simpleTpl.jsp">
                
          <put-attribute name="title" value="簡易模版"></put-attribute>
                
          <put-attribute name="header" value="/WEB-INF/jsp/common/header.jsp"></put-attribute>
                
          <put-attribute name="body" value="/WEB-INF/jsp/common/body.jsp"></put-attribute>
                
          <put-attribute name="footer" value="/WEB-INF/jsp/common/footer.jsp"></put-attribute>
            
          </definition>
            
            
          <definition name="index" extends="simpleTpl" >
                
          <put-attribute name="title" value="互聯力量首頁"></put-attribute>
            
          </definition>
          </tiles-definitions>

          5.編輯simpleTpl.jsp,將定義好的tile放入模版的對應位置。這里要用到tiles的JSP標記。使用標記需要使用taglib指令導入標記庫到頁面,需要填寫prefix 和 uri,這兩個屬性的值從對應tld文件中的short-name和uri標記的值,該文件的位置見下圖。

          simpleTpl.jsp代碼
          <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
          <%@ taglib prefix="tiles"  uri="http://tiles.apache.org/tags-tiles"%>
          <%
          String 
          path = request.getContextPath();
          String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
          %>

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
            
          <head>
              
          <base href="<%=basePath%>">
              
              
          <title>
                
          <tiles:getAsString name="title"></tiles:getAsString>
              
          </title>
              
              
          <meta http-equiv="pragma" content="no-cache">
              
          <meta http-equiv="cache-control" content="no-cache">
              
          <meta http-equiv="expires" content="0">    
              
          <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
              
          <meta http-equiv="description" content="This is my page">

            
          </head>
            
            
          <body>
              
          <tiles:insertAttribute name="header"></tiles:insertAttribute>
              
          <tiles:insertAttribute name="body"></tiles:insertAttribute>
              
          <tiles:insertAttribute name="footer"></tiles:insertAttribute>
            
          </body>
          </html>
          6.編輯struts.xml。請特別注意繼承的父包是tiles-default,result的類型是tiles。
          strust.xml代碼
          <?xml version="1.0" encoding="UTF-8" ?>
          <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
          <struts>
              
          <include file="struts-default.xml"></include>
              
          <package name="tielsDemo" namespace="/" extends="tiles-default">
                
          <action name="index">
                  
          <result type="tiles">index</result>
                
          </action>
              
          </package>
          </struts>    
          7.發布到Tomcat,啟動。地址欄輸入http://localhost:8080/tilesDemo/index.action測試。
          瀏覽器顯示結果:



          posted on 2010-05-26 13:55 javaocean 閱讀(1674) 評論(0)  編輯  收藏 所屬分類: Struts2

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 商河县| 望都县| 汉源县| 遵化市| 霍山县| 潜山县| 开原市| 漠河县| 肃北| 靖西县| 南安市| 富蕴县| 云梦县| 银川市| 芮城县| 岱山县| 叶城县| 绥芬河市| 安阳市| 福贡县| 迁安市| 油尖旺区| 缙云县| 台安县| 阿拉善左旗| 隆昌县| 香港 | 晋城| 咸宁市| 咸阳市| 玉树县| 临高县| 临桂县| 浦城县| 延庆县| 白玉县| 凌海市| 营山县| 兴安盟| 雷山县| 乐都县|