首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

SSH 整合筆記(五):整合 Tiles 2 實現頁面佈局

     編輯:Java 相關知識

在 SSH 框架整合系列的第五篇中,我們將完成 Apache Tiles 2 的整合工作。Tiles 是一個強大的頁面佈局框架,能夠將頁面拆解為可重用的組件(如頁頭、頁尾、側邊欄、內容區等),極大地提升了視圖層的可維護性與複用性。整合完成後,專案結構將呈現出清晰的模板化佈局效果。

1. 添加 Maven 依賴

首先在 pom.xml 中引入 Struts 2 的 Tiles 插件依賴,版本與 Struts 2 核心版本保持一致(此處為 2.1.6):

<dependency>
  <groupId>org.apache.struts</groupId>
  <artifactId>struts2-tiles-plugin</artifactId>
  <version>2.1.6</version>
</dependency>

2. 配置 Tiles 定義文件

在 classpath 下(通常是 src/main/resources)建立 tiles-def.xml,並確保 DTD 聲明與所用 Tiles 版本對應。此處使用 Tiles 2.0.x,故聲明為 2.0 版本:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
  "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
  "https://struts.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>
  <definition name="welcome" template="/template/layout.jsp">
    <put-attribute name="title" value="Welcome"/>
    <put-attribute name="head" value="/template/head.jsp"/>
    <put-attribute name="content" value="/template/content.jsp"/>
    <put-attribute name="foot" value="/template/foot.jsp"/>
  </definition>
</tiles-definitions>

同時,需將對應版本的 tiles-jsp.tld 放置於 /WEB-INF/ 目錄下,供 JSP 頁面引用標籤庫。

3. 建立佈局模板與片段頁面

layout.jsp(主佈局模板)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/tiles-jsp.tld" prefix="tiles" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
  <title><tiles:insertAttribute name="title" /></title>
  <META http-equiv="Content-Type" content="text/html; charset=utf-8">
</HEAD>
<BODY>
  <table>
    <tbody><tr>
      <td><tiles:insertAttribute name="head"/></td></tr>
    <tr>
      <td><tiles:insertAttribute name="content"/></td></tr>
    <tr>
      <td><tiles:insertAttribute name="foot"/></td></tr>
    </tbody></table>
</BODY>
</HTML>

head.jsp、content.jsp、foot.jsp(片段頁面)

這三個頁面分別對應頁頭、主體內容、頁尾,內容可根據業務自由定制。示例中均為靜態文本,實際開發中可引入動態數據或其他 JSP 片段。

<!-- head.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
this is head

<!-- content.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<table height="420px">
  <tr><td>this is body</td></tr>
</table>

<!-- foot.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
this is foot

4. 修改 Struts 配置啟用 Tiles 結果類型

在 struts.xml(或模組化配置如 user.xml)中,將包繼承自 tiles-default,並將成功結果類型指定為 tiles,結果值對應 Tiles 定義中的 name 屬性:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "https://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <package name="user" namespace="/user" extends="tiles-default">
    <action name="login" class="userAction" method="login">
      <result name="input">/login.jsp</result>
      <result name="success" type="tiles">welcome</result>
    </action>
  </package>
</struts>

至此,Tiles 2 整合完成。專案現已具備基於模板的頁面佈局能力,後續可擴展驗證碼、MD5 加密、富文本編輯器(如 FCKeditor)等常用功能模組。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved