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: https://issues.jboss.org/browse/AS7-2522 which caused the  "http://tiles.apache.org/tags-tiles" 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="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="struts_blank" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   <display-name>Struts Blank</display-name>
   <listener>
      <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
   </listener>
   <context-param>
      <param-name>tilesDefinitions</param-name>
      <param-value>/WEB-INF/tiles.xml</param-value>
   </context-param>
   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
   </filter>
   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>

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"
      "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
   <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>
   <definition name="/welcome.tiles" extends="baseLayout">
       <put-attribute name="title" value="Welcome page" />
       <put-attribute name="body" value="/welcome.jsp" />
   </definition>

</tiles-definitions>

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="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title><tiles:insertAttribute name="title" ignore="true" /></title>
     
</head>
<body id="home">
<div id="header">
    <tiles:insertAttribute name="header" />
</div>
<div id="wrap">
    <div id="content">
        <tiles:insertAttribute name="body" />
    </div>
    <div id="sidebar">
        <tiles:insertAttribute name="menu" />
    </div>
    <div id="footer">
        <tiles:insertAttribute name="footer" />
    </div>
</div>
</body>
</html>