Pages Navigation Menu

Coding is much easier than you think

Struts 2 and Tiles Framework Integration

Struts 2 and Tiles Framework Integration

Apache Tiles is a templating framework used to simplify the development of web application user interfaces. Tiles allow defining page fragments which can be combined into a complete page at runtime. These fragments, or tiles, can be used as reusable templates in order to reduce the duplication of common page elements or even embedded within other tiles. See the snapshot below.
** UPDATE: Struts 2 Complete tutorial now available here.
tiles layout in struts 2

Advantage of tiles framework

  • Code reusability
  • Easy to modify
  • Easy to remove

Jar Required

struts 2 - tiles library required

Folder structure

struts 2 - tiles library required


Provide entry of listener class Struts2TilesListener in the web.xml file.



Recommended Article


Jsp Pages

Now We will define the template for our web application in baseLayout.jsp file. This template will contain different segments of web page (Header, Footer, Menu, Body).
File : baseLayout.jsp

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><tiles:insertAttribute name="title" ignore="true" /></title>
.one {
	border-style: solid;
	border-color: #0000ff;
	border-collapse: collapse;
<table border="1" align="center" class="one" width="80%">
		<td height="30" colspan="2" class="one" width="20%">
			<tiles:insertAttribute name="header" />
		<td height="250" class="one">
			<tiles:insertAttribute name="menu" />
		<td width="350" class="one">
	    	<tiles:insertAttribute name="body" />
		<td height="30" colspan="2" class="one">
	        <tiles:insertAttribute name="footer" />

File : header.jsp

<div align="center" style="color: gray; font-weight: bold;">
Struts 2 - Tiles Demo - Header page

File : footer.jsp

<div align="center" style="color: gray; font-weight: bold;">
Footer	page &copy; SimpleCodeStuffs.com

File : body.jsp

	<p align="center" style="color: gray;font-weight: bold;">Home Page</p>

File : menu.jsp

<%@taglib prefix="s" uri="/struts-tags"%>
	<s:url action="strutsAction" var="strutsAction" />
	<s:url action="springAction" var="springAction" />
	<div align="center">
		Menu <br /> <br /> <br /> <br />

		<s:a href="%{strutsAction}">Struts Tutorial</s:a>
		<br /> <br />
		<s:a href="%{springAction}">Spring Tutorial</s:a>

File : springTutorial.jsp

<p align="center" style="color: gray;font-weight: bold;">Spring Tutorial !!!</p>

File : strutsTutorial.jsp

	<p align="center" style="color: gray;font-weight: bold;">Struts Tutorial !!!</p>

File : index.jsp
It triggers homeAction during application start up

<META HTTP-EQUIV="Refresh" CONTENT="0;URL=homeAction">


Action Class


package com.simplecodestuffs.action;

public class TilesAction {

	public String home() {
		return "home";

	public String struts() {
		return "struts";

	public String spring() {
		return "spring";



In struts.xml, inherit the tiles-default package and define all the result type as tiles

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

<package name="default" extends="tiles-default">
	<action name="*Action" method="{1}" class="com.simplecodestuffs.action.TilesAction">
		<result name="home" type="tiles">home</result>
		<result name="struts" type="tiles">struts</result>
		<result name="spring" type="tiles">spring</result>

Do read :



The tiles.xml file must be located inside the WEB-INF directory, in which define all the tiles definitions

<?xml version="1.0" encoding="UTF-8" ?> 

<!DOCTYPE tiles-definitions PUBLIC 
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" 


<definition name="baseLayout" template="/baseLayout.jsp">
	<put-attribute name="title" value="Template" />
	<put-attribute name="header" value="/header.jsp" />
	<put-attribute name="menu" value="/menu.jsp" />
	<put-attribute name="body" value="/body.jsp" />
	<put-attribute name="footer" value="/footer.jsp" />

<definition name="home" extends="baseLayout">
	<put-attribute name="title" value="Home" />
	<put-attribute name="body" value="/home.jsp" />

<definition name="struts" extends="baseLayout">
	<put-attribute name="title" value="Struts Tutorial" />
	<put-attribute name="body" value="/strutsTutorial.jsp" />

<definition name="spring" extends="baseLayout">
	<put-attribute name="title" value="Spring Tutorial" />
	<put-attribute name="body" value="/springTutorial.jsp" />




Home Page with Tiles
struts 2 tiles home page
Struts Page with Tiles
struts 2 tiles
Spring Page with Tiles
tiles struts 2

How to define multiple tiles files in struts 2 application?

To define multiple tiles, you need to add following entry in your web.xml file.

<context-param id="struts_tiles">  


Download It – Struts2-Tiles


About Mohaideen Jamil

I'm a Full stack developer of IT exp in J2EE, AngularJs, MicroServices, Docker, Spring (Boot, MVC, Cloud), Bluemix, DevOps. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

%d bloggers like this: