Struts 2 Tutorial Series – How to insert Tiles to your project

This tutorial is based on Struts 2 Tutorial Series - Login application.

1. What is Tiles?

Apache Tiles is a templating framework built to simplify the development of web application user interfaces. Tiles allows you to define page fragments which can be assembled into a complete page at runtime. See the snapshot below.

2. Goal of this application

We will add Tiles to our Login application. After a user login,

a well-formatted page will displays like this. You can download the code

3. Tools and libraries required

This application is based on the login application, it use the same environment.

1. JDK 1.5 or above
2. Tomcat 6.0
3. Eclipse IDE for Java EE Developers
4. Struts 2.0.14 (link). JAR files required for this application. Download Link 1.
Download link 2(permanent on
5. Libraries used.

4. Getting Started

This is based on our Login application. If you haven't walk through it, you can do it now. Or you can also get the required file by going back to the tutorial or downloading this package.

5. Configuring Tiles in web.xml

To configure Tiles, an entry for listener has to be made in web.xml. Open the web.xml from WEB-INF folder and add following code into it.



Here is the finally code.


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
	xmlns="" xmlns:web=""
	id="WebApp_ID" version="2.5">
	<display-name>Struts2 Login</display-name>

6. Configuring tiles.xml file

Create a file tiles.xml in WEB-INF folder and copy following code into it.


<?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="" />
		<put-attribute name="header" value="/Header.jsp" />
		<put-attribute name="menu" value="/Menu.jsp" />
		<put-attribute name="body" value="" />
		<put-attribute name="footer" value="/Footer.jsp" />
	<definition name="/welcome.tiles" extends="baseLayout">
		<put-attribute name="title" value="Welcome page" />
		<put-attribute name="body" value="/Welcome.jsp" />
	<definition name="/error.tiles" extends="baseLayout">
		<put-attribute name="title" value="Error page" />
		<put-attribute name="body" value="/Error.jsp" />

In the tiles.xml file, we have define a baseLayout using template BaseLayout.jsp. This layout contains attributes such as header, title, body, menu and footer. The layout is then extended and new definitions for Welcome page and Error page is defined. When We override the base layout, only those changed attribute will be change to be different with base layout. All other attribute remains the same with base layout.

7. Struts.xml file and LoginAction Class

To demonstrate tiles, we add a logout function to LoginAction class.

package com.programcreek.struts2;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
	private String username;
	private String password;
	public String authenticate() {
		if (this.username.equals("admin")
				&& this.password.equals("admin")) {
			return "success";
		} else {
			return "error";
	public String logout() {
			return "logout";
	public String getUsername() {
		return username;
	public void setUsername(String username) {
		this.username = username;
	public String getPassword() {
		return password;
	public void setPassword(String password) {
		this.password = password;

In struts.xml file, we add tags to the package to invoke tiles to handle requests.


<?xml version="1.0" encoding="UTF-8" ?>
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
	<constant name="struts.enable.DynamicMethodInvocation"
		value="false" />
	<constant name="struts.devMode" value="false" />
	<constant name="struts.custom.i18n.resources"
		value="ApplicationResources" />
	<package name="default" extends="struts-default" namespace="/">
			<result-type name="tiles"
				class="org.apache.struts2.views.tiles.TilesResult" />
		<action name="login" method="authenticate"
			<result name="success" type="tiles">/welcome.tiles</result>
			<result name="error" type="tiles">/error.tiles</result>
		<action name="logout" method="logout" 
			<result name="logout">/Login.jsp</result>

8. Creating JSP files

Here is all JSP files.


<%@ taglib uri="" 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>
	<link rel="stylesheet" type="text/css" media="all" href="style.css" />
<body id="home">
<div id="header">
	<tiles:insertAttribute name="header" />
<div id="wrap">
	<div id="content">
		<tiles:insertAttribute name="body" />
	<div id="sidebar">
		<tiles:insertAttribute name="menu" />
	<div id="footer">
		<tiles:insertAttribute name="footer" />


error page.


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<h1>Struts 2 Example</h1>


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<title>Struts 2 - Login Application</title>
<h2>Struts 2 - Login Application</h2>
<s:actionerror />
<s:form action="login.action" method="post">
	<s:textfield name="username" key="label.username" size="20" />
	<s:password name="password" key="label.password" size="20" />
	<s:submit method="execute" key="label.login" align="center" />


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:url id="testUrlId" namespace="/" action="logout">
<s:a errorText="Sorry your request had an error."

Here, be careful about how to define a link to trigger an action.


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<title>Welcome Page - Struts 2 - Login Application</title>
	<h2>Congratulations, <s:property value="username" />!</h2>
	Welcome to Struts 2 world.
	<h3>What is Apache Tiles</h3>
	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. These templates streamline the
	  development of a consistent look and feel across an entire 
	  application. Tiles grew in popularity as a component of the
	   popular Struts framework. It has since been extracted from 
	   Struts and is now integrated with various frameworks, such 
	   as Struts 2 and Shale.


@CHARSET "ISO-8859-1";
#header {
  border:1px solid blue;
  background-position:50% 0;
  background-repeat:repeat no-repeat;
  padding: 10px;
#wrap {
#content {
  border:1px solid blue;
  min-height: 500px;
#sidebar {
  border:1px solid blue;
  min-height: 500px;
#footer {
  border:1px solid blue;
  background-position:50% 100%;
  background-repeat:repeat no-repeat;

style.css is used for styling the layout.

9. We are done now

The file structure should look like this.

10. Run application

Login page

Welcome page

References: Servlet filters and Event Listeners

Category >> Struts 2  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";
  • ritendra

    Are you willing to become a mountaineer ? Do you want to embark on the journey that many do not because of fear ? If yes, this trek and Peak climbing is for you. Island Peak Climbing with Everest base camp is for adventurous souls looking to conquer fear. Island Peak is one of the first level peaks in Nepal and often a practice peak for those attempting Everest. Although it may be around 6000 meter in elevation, it is no ordinary peak and will put you on your knees if you let it.
    please visit this website for the best trekking agencies in kathmandu at affordable price.

  • Nepali Ritendra

    visit www, for the best hotel in kathmandu at affordable price.

  • Alex

    Nice tutorial 🙂
    But cant download the code though 🙁

  • Munish

    Help me.
    can i directly run Tiles Layout page .. without index.jsp page when i deployed project..
    mail me on [email protected]

  • Sunil

    Hi I am facing following exceptions with above

    at org.apache.catalina.core.ApplicationFilterConfig.(
    at org.apache.catalina.core.StandardContext.filterStart(

    and many more exceptions are throwing.

    Please tell me what is the problem…

  • Admin

    Thanks for your comments. 🙂

  • Hello Sir,
    Really the above example are very useful for struts biggnners,

  • swathi

    hi this is new to struts2.can u give me detail description of struts2 how it works .how action works how return value is retured and all those basic things

  • Hey man I just wanted to say thanks for taking the time to write something worth my time to read. I am all over the net and I see so much pointless content that is just written for the sake of putting something new on their page. It takes devotion to create good stuff, thanks for caring.

  • I am little confused how you attach style.css with jquery ??

  • Anja Vanhekken

  • nice and well written tutorial.

  • The good storage of facts referring to this topic people search, at the freelance writing service.

  • I had got a dream to start my own commerce, but I didn’t earn enough of cash to do that. Thank heaven my fellow recommended to utilize the loan. Hence I used the auto loan and realized my dream.

  • Many other World of Warcraft gold would do well to take notes on how the Warcraft guide illustrates the art of World of Warcraft gold making into no risk and easy to follow steps.These World of Warcraft player are very efficient in buying cheap wow gold. For many of these gold makers, this is their real job.