My name is Vasyl Khrystiuk‎ > ‎process‎ > ‎main‎ > ‎



Apache Tiles is implementation of Core J2EE Pattern - Composite View.

It's bad idea to do something without knowing - what you want to get.
So, this is our test layout:

1) Add libraries (via pom.xml):

<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts-tiles</artifactId> <version>1.3.10</version> </dependency>

2) Idea is in creating html(xml) pages from parts(tiles), each part(tile) can consist of content and or other tiles.  In this way wee have strong template engine. We can easyly change content of any tile and this changes will be wisible on any page that uset this tampleate(layout).

This is our main tamplate tile(layouts\mainLayout.jsp):
<%@taglib uri="" prefix="bean"%>
<%@taglib uri="" prefix="c" %>
<%@ taglib uri="" prefix="fn" %>
<%@taglib uri="" prefix="tiles" %>
<!DOCTYPE html>
    <title><tiles:getAsString name="title" ignore="true" /></title>
    <div id="pageContainer">
        <div id="header">
            <tiles:insert attribute="header" ignore="true"/>
             <div id="user_stat">
                 <tiles:insert attribute="userstat" ignore="true"/>
        <div id="pageEntry">
            <div id="menu">
                <tiles:insert attribute="menu" ignore="true"/>
            <div id="content">
                <tiles:insert attribute="content" ignore="true"/>
        <div id="footer">
            <tiles:insert attribute="footer" ignore="true"/>
This JSP page is tile. We write actualy html, but some parts are not implement. We will use this file as layout.
Let's apply  this layout to main site page(index.jsp):
<%@page contentType="text/html; charset=UTF-8" %>
<%@taglib uri="" prefix="c" %>
<%@taglib prefix="fmt" uri="" %>
<%@taglib uri="" prefix="tiles" %>

<tiles:insert page="/layouts/mainLayout.jsp" flush="true">
     <tiles:put name="title" value="Page title" type="string"/>
     <tiles:put name="header" type="string">
        <tiles:insert page="/includes/header.jsp" flush="false"/>
     <tiles:put name="userstat" type="string">
        <tiles:insert page="/includes/status.jsp" flush="false"/>
     <tiles:put name="menu" type="string">
        <tiles:insert page="/includes/menu.jsp" flush="false"/>
     <tiles:put name="content" type="string">
        <tiles:insert page="/content/main.jsp" flush="false"/>
     <tiles:put name="footer" type="string">
        <tiles:insert page="/includes/footer.jsp" flush="false"/>
All that left to do is to create files that use as tiles and fill them with some content.
These are they: /includes/header.jsp, /includes/status.jsp, /includes/menu.jsp, /content/main.jsp, /includes/footer.jsp.

In my case no additional configuration was needed (I just create template and add taglib in file header. However I not sure will it work everywhere, so better see instalation notes on tileses site:

Also possible define definitions in JSP:
    <tiles:put name="paramet" value="sfsdf" />
<tiles:insert beanName="storefront.default" beanScope="request"> 
<%@taglib uri="" prefix="tiles" %>
<tiles:getAsString name="paramet" ignore="true" />
Note: tiles:definition and tiles:insert beanName must be scope visible, so better to define definition in file for definitions or use some like: 
<%@include file="../common/storefront-defs.jsp" %>

And now more tech details:

Also tiles:definition  are accessible (however i do not find reason to use them, but its better to know about them)
    tiles:definition can be nestings.
    tiles:definition can be abstract.
    tiles:definition can extends.
    tiles:definition can be chenged before rendering via ViewPreparer subcalss and preparer property thats point to this class.
Tiles can be changed and compose in runtime (
Tiles can be used without supported framework( struts support ) via TilesDispatchServlet.
Also tiles can be used as rendering decorator via TilesDecorationFilter.
Tiles support localization.(But i prefer to use  struts:bean library).
Tiles can use beans as attributes.
>>Tiles can even use with Spring!

Used sources:


Make styling yourself =)

Vasyl Khrystiuk,
10 Jan 2012, 08:12
Vasyl Khrystiuk,
11 Jan 2012, 17:44