Struts2 and Tiles tutorial

User Rating: 4 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Inactive

Would you like to run a Struts2 application powered by Apache tiles on JBoss AS 7 ? then this tutorial is for you! 

Apache 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 reusable templates in order to reduce the duplication of common page elements or even embedded within other tiles.

Apache Tiles grew in popularity as a component of the popular Struts framework and is now integrated with various frameworks, such as Struts 2.

In one of my first attempts to run Struts2 + Tiles on JBoss AS 7 I've met the following issue: which caused the  "" taglib not being recognized. Let's see it on JBoss AS 7.1.1

In order to configure Apache tiles, you need at first to add an entry for Tiles listener in web.xml. Following here is the web.xml file which contains both the Tiles listener and the Struts2 dispatcher:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="" xmlns:xsi="" id="struts_blank" version="2.4" xsi:schemaLocation="">
   <display-name>Struts Blank</display-name>

As you can see from the above code an input configuration file /WEB-INF/tiles.xml is passed as argument. This file contains the Tiles definition for our web application.
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="/layout.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" />


Here in tiles.xml we have define a template baseLayout. This layout contains attributes such as Header, Title, Body, Menu and Footer. The layout is then extended and new definitions for Welcome page is defined. We have override the default layout and changed the content for Body and Title.

Adding the JSPs to the project

The first JSP we will add is layout.jsp. This template will contain the different segments used in the web page (Header, Footer, Menu etc).

<%@ 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>
<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" />