Saturday, April 11, 2009

Sample Tiles Application

Apache Tiles is a templating framework built to simplify the development of web application user interfaces.Tiles allows authors to define page fragments which can be assembled into a complete page at runtime.These fragments, or tiles, can be used as simple includes in order to reduce the duplication of common page elements or embedded within other tiles to develop a series of reusable templates.

Lets build a sample tiles app

Configuraion using web.xml



Also the configuration can be done using spring xml as follows

<bean id="tilesConfigurer"
<property name="definitions">


Define a tiles.xml and place it under WEB-INF


<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"

<definition name="main" template="/layouts/template.jsp">
<put-attribute name="logo" value="/tiles/logo.jsp" />
<put-attribute name="navigation" value="/tiles/navigation.jsp" />
<put-attribute name="body" value="/tiles/indexBody.jsp" />
<put-attribute name="footer" value="/tiles/footer.jsp" />

<definition name="offer" template="/layouts/template.jsp" extends="main">
<put-attribute name="body" value="/tiles/offerBody.jsp" />


Tiles also helps in inheritance as you can see

The offer tile definition extends the main definition and can override any of parts of the main definition.

The template looks as follows and can be changed for look and feel


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<%@ taglib uri="" prefix="tiles" %>
<html xmlns="" lang="en" xml:lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Discount Sale</title>
<div id="main">
<tiles:insertAttribute name="logo"/>
<tiles:insertAttribute name="navigation"/>
<tiles:insertAttribute name="body"/>
<tiles:insertAttribute name="footer"/>

All other jsps like logo.jsp , navigation.jsp and footer.jsp can have the logic for view as in any othet jsps.

Bug when Tomcat 6(installed in directory with spaces) and Xwork

When tomcat is installed in a directory with spaces , and used for struts application you may get a



This will be fixed in the future versions of the xwork jar , else you need to download the latest code from their branch and build the jar.

There is alternative to this , this error can be avoided by setting


Free Domain Names @!