Struts2 and Tiles Tutorial

This updated tutorial shows how to build and deploy on WildFly a Struts 2 application powered by Apache Tiles, a template framework for Java Web applications.

Pre-requisites: we recommend checking this tutorial to learn how to deploy Struts2 applications on WildFly: Getting started with Struts 2 on WildFly

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.

To enable Apache tiles, you need at first some configuration steps in web.xml file. Then, you need to add a specific configuration file (tiles.xml) for this framework. The next section discusses this step.

Configuring Struts 2 and Apache Tiles

Firstly, to configure Apache tiles, you need to add an entry for Tiles listener in web.xml. Following here is the web.xml file which contains both the StrutsTiles 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, we need to include three main items in our configuration:

  • The StrutsTilesListener to automatically tie Tiles support into Struts
  • The StrutsPrepareAndExecuteFilter which handles both the preparation and execution phases of the Struts dispatching process
  • Finally, we pass as argument to the above filter the configuration file /WEB-INF/tiles.xml. This file contains the Tiles definition for our web application.


If you are using Struts 2 version 2.5 you need to change from: to:

Next, let’s add the tiles.xml:

<?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" />


This configuration file sets up the default layout (baseLayout) which includes Title, Header, Menu, Body and Footer. Besides, it also adds another definition (welcome.tiles) that extends the main template and just set its body.

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

Next, create the following JSP files with the following content in each of them.

<div style="padding:16px;background-color:blue">
<h3>This is the Page Header</h3>

Then, include the footer.jsp

<div style="padding:16px;background-color:red">
<h3>This is the Page Footer</h3>

Then, include the menu.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:url  namespace="/" action="logout.action" value="To exit ->" />

<s:url  action="logout.action" var="urlTag" />

<s:a href="%{urlTag}">Logout</s:a>

<s:a errorText="Sorry your request had an error."


Finally, include the welcome.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<title>Struts 2 - Tiles example Application</title>
    <h2>Welcome, <s:property value="username" />!</h2>
    Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor 
    incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, 
    quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi 
    Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu 
    fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident,
    sunt in culpa qui officia deserunt mollit anim id est laborum.

As you can see the welcome page dumps a property username which derives from the following index.jsp page:

<%@ 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:form action="login.action" method="post">
    <s:textfield name="username" label="Username" size="20" />
    <s:password name="password" label="Password" size="20" />
    <s:submit method="execute" label="Login" align="center" />

Coding the Struts 2 action

In Struts terms, Action classes act as the controller in the MVC pattern. Action classes respond to a user action, execute business logic and then return a result that tells Struts what view to render.

Here is the Struts2 Login action which authenticates the user and returns the appropriate action:

package struts2.example;

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;

Struts 2 Configuration

The main configuration file of a Struts2 application is struts.xml. You need to place this file in the application’s classpath. For example, in a Maven project, place it under src/main/resources. Following up the build phase, the Maven builder will place it into WEB_INF/classes.

<?xml version="1.0" encoding="UTF-8" ?>
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
  <constant name="struts.devMode" value="true"/>
    <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">/index.jsp</result>
        <action name="logout" method="logout" 
            <result name="logout">/index.jsp</result>

Building the application

In order to build and package your application, you can use the following pom.xml file which contains the dependencies for Jakarta EE 8 applications along with Struts2 and Tiles dependencies:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">
    <name>struts-demo Maven Webapp</name>



Testing the application

Next, deploy the application on WildFly. Upon requesting the welcome page, the Login screen will appear:

Enter “admin” / “admin” to continue. You will be redirected to the Welcome page:


This article was a walk through the design and configuration of a simple Struts 2 application which uses Apache Tiles as templating framework.

Source code: