Struts 2 Tutorial, Part 5 - Struts 2 Interceptors Tutorial With Example
Struts 2 Tutorial, Part 5 - Struts 2 Interceptors Tutorial With Example
com/print/16771
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 [1]. 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.
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.
1 of 8 20/09/2010 8:53 PM
Struts 2 Tutorial: Struts 2 Tiles Plugin Tutorial with Example in Eclipse http://java.dzone.com/print/16771
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
2 of 8 20/09/2010 8:53 PM
Struts 2 Tutorial: Struts 2 Tiles Plugin Tutorial with Example in Eclipse http://java.dzone.com/print/16771
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.
</definition>
<definition name="/welcome.tiles" extends="baseLayout">
<put-attribute name="title" value="Welcome" />
</definition>
<definition name="/customer.success.tiles" extends="baseLayout">
<put-attribute name="title" value="Customer Added" />
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
3 of 8 20/09/2010 8:53 PM
Struts 2 Tutorial: Struts 2 Tiles Plugin Tutorial with Example in Eclipse http://java.dzone.com/print/16771
We 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>
<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"%>
Menu.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
Footer.jsp
4 of 8 20/09/2010 8:53 PM
Struts 2 Tutorial: Struts 2 Tiles Plugin Tutorial with Example in Eclipse http://java.dzone.com/print/16771
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" ?>
<struts>
<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<action name="login"
class="net.viralpatel.struts2.LoginAction">
<action name="customer"
class="net.viralpatel.struts2.CustomerAction">
<result name="success" type="tiles">/customer.success.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.
5 of 8 20/09/2010 8:53 PM
Struts 2 Tutorial: Struts 2 Tiles Plugin Tutorial with Example in Eclipse http://java.dzone.com/print/16771
6 of 8 20/09/2010 8:53 PM
Struts 2 Tutorial: Struts 2 Tiles Plugin Tutorial with Example in Eclipse http://java.dzone.com/print/16771
Moving On
Today we saw how we can configure Tiles framework with Struts2 application. In next part [5] 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.
Links:
[1] http://java.dzone.com/articles/struts2-tutorial-part-37
[2] http://java.dzone.com/articles/struts2-tutorial-part-17
[3] http://java.dzone.com/articles/struts2-tutorial-part-27
[4] http://java.dzone.com/articles/struts2-tutorial-part-47
[5] http://java.dzone.com/articles/struts2-tutorial-part-57
[6] http://java.dzone.com/articles/struts2-tutorial-part-67
[7] http://java.dzone.com/articles/struts2-tutorial-part-77
[8] http://viralpatel.net/blogs/2008/12/tutorial-struts-tiles-plugin-example-in-eclipse.html
7 of 8 20/09/2010 8:53 PM
Struts 2 Tutorial: Struts 2 Tiles Plugin Tutorial with Example in Eclipse http://java.dzone.com/print/16771
[9] http://viralpatel.net/blogs/download/struts/Part-4-StrutsHelloWorld.zip
[10] http://viralpatel.net/blogs/
8 of 8 20/09/2010 8:53 PM