Struts Tiles framework is a layout framework, which allow users to maintain a standard look of header, footer and menu across all of your web pages efficiently.

Download this example –

Tiles template example

Here’s an example to create a tiles template to maintain the header and footer details across all of the web pages in Struts.

Firstly, see this Struts tiles framework relationship.

1. Get Struts Tiles Library

Get the struts tiles library from the Struts distribution folder, or via Maven central repository


and include it in your project dependency library.

2. Create a Template

Create a template red and template green for header and footer details. These two templates are just pure HTML code with different background color..

Template – Red color

<div style="padding:16px;background-color:red">
<h1>[Logo Here] This is Template Red Header</h1>


<div style="padding:16px;background-color:red">
<h1>This is Template Red Footer</h1>

Template – Green color

<div style="padding:16px;background-color:green">
<h1>[Logo Here] This is Template Green Header</h1>


<div style="padding:16px;background-color:green">
<h1>This is Template Green Footer</h1>

3. Tiles layout

Create a standard web page layout for all of your web pages.


<%@ taglib uri="" prefix="tiles" %>
<tiles:insert attribute="header"/>
<tiles:insert attribute="body"/> 
<tiles:insert attribute="footer"/> 

4. Body Template

In the body template, you should always create two pages “user-form.jsp and user-form-body.jsp” for body details to break the coupled with tiles framework. The “user-form.jsp” is used to get the tiles definition, and “put” the real body content (user-form-body.jsp) as body template.


<%@ taglib uri="" prefix="tiles" %>
<tiles:insert definition="company-template" >
	<tiles:put name="body" value="/pages/user/body/user-form-body.jsp" />


<div style="padding:16px;background-color:blue;height:200px;">
<h1>This is body content </h1>

5. Tiles Definition

All the templates are done, create a “tiles-defs.xml” file and declared a “company-template” definition for red template.


<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 1.3//EN"
   <definition name="company-template" path="/pages/tiles/common-layout.jsp">
	<put name="header" value="/pages/tiles/template-red/header.jsp" />
	<put name="footer" value="/pages/tiles/template-red/footer.jsp" />

6. Include the TilesPlugin

To use Struts tiles framework, you have to declare the “TilesPlugin” plug-in class in the Struts configuration file.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
	<plug-in className="org.apache.struts.tiles.TilesPlugin" >
		<set-property property="definitions-config"

7. Demo

In above case, the template red is used.



To change it to template green, you just need to update the “tiles-defs.xml” file.


<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 1.3//EN"
   <definition name="company-template" path="/pages/tiles/common-layout.jsp">
	<put name="header" value="/pages/tiles/template-green/header.jsp" />
	<put name="footer" value="/pages/tiles/template-green/footer.jsp" />

Access it again



The header and footer color is changed (template green), with just a minor changes in the tiles configuration file.


Struts Tiles Documentation –

Tags :
Founder of, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.
My website is hosted by Liquid Web, I really like their fast and helpful support, it saved me a lot of time. Furthermore, their servers are extremely reliable, fast and stable, I don't remember when was the last time we had server downtime. Btw, they're offering $100 Coupon now, if you are sourcing for great hosting provider, go grab it.

Related Posts

Popular Posts