Viral Patel is a keen technology blogger and enthusiast. Working with a software company, he uses his leisure time to learn and explore emerging areas in tech world. His passion for technology has motivated him from creating an operating system to writing tutorials/articles on Java, JavaEE, Spring, Hibernate, Struts, jQuery, PHP. Visit the website http://viralpatel.net for a wide range of articles and tutorials on these topics. Viral has posted 13 posts at DZone. View Full User Profile

Struts 2 Tutorial: Struts 2 Tiles Plugin Tutorial with Example in Eclipse

01.17.2010
| 93006 views |
  • submit to reddit

Welcome to Part-4 of the 7-part series where we will go through different aspects for Struts2 Framework with some useful examples. In previous part we went through Struts2 Validation Framework. We saw how easy it is to integrate validation in your struts2 application.

In this part we will discuss about Tiles Framework and its Integration with Struts2. We will add Tiles support to our HelloWorld Struts application that we created in previous parts. I strongly recommend you to go through previous articles and download the source code of our sample application.

Struts 2 Tutorial List

 
 Related: Struts Tiles Plugin Tutorial with Example

Introduction to Tiles 2

Nowadays, website are generally divided into pieces of reusable template that are being rendered among different web pages. For example a site containing header, footer, menu etc. This items remains same through out the website and give it a common look and feel. It is very difficult to hard code this in each and every webpage and if later a change is needed than all the pages needs to be modified. Hence we use templatization mechanism. We create a common Header, Footer, Menu page and include this in each page.

Tiles Plugin allow both templating and componentization. In fact, both mechanisms are similar: you
define parts of page (a “Tile”) that you assemble to build another part or a full page. A part can
take parameters, allowing dynamic content, and can be seen as a method in JAVA language. Tiles is a templating system used to maintain a consistent look and feel across all the web pages of a web application. It increase the reusability of template and reduce code duplication.

A common layout of website is defined in a central configuration file and this layout can be extended across all the webpages of the web application.

Our Application Layout

Our goal is to add Header, Footer and Menu to our StrutsHelloWorld application. Following will be the layout of the same.
struts2-tiles-layout

Required JAR files

In order to add Tiles support to our Struts2 application, we will need few jar files. Following is the list of JARs in our example. Add these JARs in WEB-INF/lib folder.
struts2-tiles-jar-files

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.

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

The above code configure Tiles listener in web.xml. 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.
struts2-tiles-xml

<?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="/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>
<definition name="/welcome.tiles" extends="baseLayout">
<put-attribute name="title" value="Welcome" />

<put-attribute name="body" value="/Welcome.jsp" />
</definition>
<definition name="/customer.tiles" extends="baseLayout">

<put-attribute name="title" value="Customer Form" />
<put-attribute name="body" value="/Customer.jsp" />

</definition>
<definition name="/customer.success.tiles" extends="baseLayout">
<put-attribute name="title" value="Customer Added" />

<put-attribute name="body" value="/SuccessCustomer.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 and Customer page is defined. We have override the default layout and changed the content for Body and Title.

Creating JSPs

struts-2-tiles-layout-jspWe will define the template for our webapplication in a JSP file called BaseLayout.jsp. This template will contain different segments of web page (Header, Footer, Menu etc). Create 4 new JSP files BaseLayout.jsp, Header.jsp, Menu.jsp and Footer.jsp and copy following content in each of them.
BaseLayout.jsp

<%@ 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>
<table border="1" cellpadding="2" cellspacing="2" align="center">
<tr>

<td height="30" colspan="2"><tiles:insertAttribute name="header" />
</td>
</tr>

<tr>
<td height="250"><tiles:insertAttribute name="menu" /></td>
<td width="350"><tiles:insertAttribute name="body" /></td>

</tr>
<tr>
<td height="30" colspan="2"><tiles:insertAttribute name="footer" />

</td>
</tr>
</table>
</body>
</html>

Header.jsp

<%@ page contentType="text/html; charset=UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>
<h2>Struts2 Example - ViralPatel.net</h2>

Menu.jsp

<%@ page contentType="text/html; charset=UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>
<s:a href="customer-form">Customer</s:a>

Footer.jsp

<%@ page contentType="text/html; charset=UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>
Copyright &copy; ViralPatel.net

Modifications in Struts.xml

In struts.xml we defined result tag which maps a particular action with a JSP page. Now we will modify it and map the result with Tiles. Following will be the content of struts.xml file.

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

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<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-types>
<result-type name="tiles"

class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<action name="login"
class="net.viralpatel.struts2.LoginAction">

<result name="success" type="tiles">/welcome.tiles</result>
<result name="error">Login.jsp</result>
</action>

<action name="customer"
class="net.viralpatel.struts2.CustomerAction">
<result name="success" type="tiles">/customer.success.tiles</result>

<result name="input" type="tiles">/customer.tiles</result>
</action>
<action name="customer-form">
<result name="success" type="tiles">/customer.tiles</result>

</action>
</package>
</struts>

The struts.xml now defines a new Result type for Tiles. This result type is used in <result> tag for different actions. Also note that we have define a new action customer-form. This is just an empty declaration to redirect user to Customer form page when she clicks Customer link from menu.

That’s All Folks

Compile and Execute the application in Eclipse and see that the header, menu and footer are properly applied.
Welcome Page with Tiles
struts-2-welcome-page-tiles
Customer Page with Tiles
struts2-tiles-customer-page
Customer Success Page with Tiles
struts2-customer-added-tiles

Download Source Code

Click here to download Source Code without JAR files (11KB)

Moving On

Today we saw how we can configure Tiles framework with Struts2 application. In next part we will discuss about Struts2 Interceptors and see example of it. I hope you liked this article. Feel free to post your queries and comments in comment section.

 

From: http://viralpatel.net/blogs/

 

Published at DZone with permission of its author, Viral Patel.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Robert Heath replied on Thu, 2010/12/02 - 8:25am

Unfortunately, the group of Struts jar files are spread over various versions of Struts. 

And unless you use this specific group of jar files, you are likely to get exceptions due to some jar files being incompatible with each other. :(

If you are unable to locate a struts jar file in a specfic release of Struts, you can do a search for the specific jar file and find it in a search engine.

Marco Dessy replied on Sun, 2010/12/05 - 12:01pm

There's an error in Menu.jsp: - customer-form + customer-form.action Bye

Manmohan Vyas replied on Sat, 2011/02/12 - 9:01am

@Macro yup u r right
here goes the solution.



Menu.jsp
href="lt s:url action="customer-form"/ gt"

Anjib Mulepati replied on Sun, 2011/05/15 - 10:37pm

If I run this program I just get login page. I change to have BaseLayout.jsp as welcome page it get error: Attribute 'header' not found.

Paul Gorbas replied on Mon, 2011/06/20 - 5:58pm

What a complete mess -

 

The specific set of lib listed in this example are NOT abailable in any sigle package - it is a hodge-pdoge of several diffrent versions

 

The versions of the lib listed ARE NOT in the structs dl in part 2 of this tutorial.

Paul Gorbas replied on Mon, 2011/06/20 - 6:43pm

After carefully collecting all the various lib, dling each one individually, and insuring the versions exactly match whats was in the tutorial - it still fails - not even finding common struts tags - yes I did a clean..

Anders Norell replied on Sun, 2011/10/16 - 11:00am

Great job!
The example worked after adjusting Menu.jsp:
<s:a href="customer.action">Customer</s:a>
and
Welcome.jsp
<s:a href="customer.action">Add customer</s:a>

Chalapathi Katta replied on Tue, 2012/05/15 - 1:05am

  Hi, I develop the the above tiles project in eclipse but show the below error in my console. Tell me solution for the problem   SEVERE: Error configuring application listener of class         org.apache.struts2.tiles.StrutsTilesListener    java.lang.ClassNotFoundException:         org.apache.struts2.tiles.StrutsTilesListener     

Farah Fertassi replied on Sun, 2012/07/15 - 11:53am

I'm using Eclipse and Tomcat6. When I import the project and i run it i get : HTTP 404 error.. any idea abt what can be the matter??

Farah Fertassi replied on Sun, 2012/07/15 - 1:13pm in response to: Farah Fertassi

That's fine, two jars were missing even if there were no indications abt the missing jars although the devMode set on true.., so here they are all the necessary jars to make Tiles work with struts:  commons-beanutils, commons-collections, commons-digester, struts2-tiles-plugin, tiles-api, tiles-core, tiles-jsp

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.