Total Pageviews

2011/07/13

Apache Tiles Quick Start

Tiles concepts
Tiles is an implementation of the Composite View pattern. Tiles adds to this pattern its own concepts to make the pattern concrete. The implementation of Tiles around the Composite View pattern consists of the Template, Attribute and Definition concepts. The View Helper pattern is implemented by the View Preparer concept.

Template
In Tiles, a template is the layout part of a page. You can see as a page structure with some gaps, called attributes, to be filled.

For instance, consider the page structure.


You can replicate this structure by creating a JSP page, as you can see below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<html>
   <head>
      <title>
         <tiles:getAsString name="title"/>
      </title>
      <meta name="pagebase" content="<tiles:insertAttribute name="pagebase"/>
      "/>
      <meta name="funccode" content="<tiles:insertAttribute name="funccode"/>
      "/>
      <!-- ... -->
   </head>
   <body>
      <a name="top" ></a>
      <tiles:insertAttribute name="header" />
      <tiles:insertAttribute name="menu" />
      <div id="contentBase">
         <form>
            <!-- ... -->
         </form>
         <tiles:insertAttribute name="content" />
      </div>
   </body>
</html>


tiles-config.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<!-- 裁處書 預設樣板 -->
<definition name="punishmentForm" template="/WEB-INF/jsp/common/punishmentFormMain.jsp">
   <put-attribute name="pagebase" value="nig" type="string" />
   <put-attribute name="funccode" value="Template" type="string" />
   <put-attribute name="title" value="Template" type="string" />
   <put-attribute name="header" value="/WEB-INF/jsp/common/header.jsp" />
   <put-attribute name="menu" value="/WEB-INF/jsp/common/menu.jsp" />
   <put-attribute name="content" value="/WEB-INF/jsp/common/content.jsp" />
</definition>
<!-- extends "punishmentForm" template -->
<definition name="/front/NIG451W" extends="punishmentForm">
   <!-- override funccode -->
   <put-attribute name="funccode" value="NIG451W" type="string" />
   <!-- override title -->
   <put-attribute name="title" value="營所稅基本稅額" type="string" />
   <!-- override content --> 
   <put-attribute name="content" value="/WEB-INF/jsp/front/punishment/NIG451W.jsp" />
</definition>


Check the result

No comments: