Struts 2 Tiles Plugin Tutorial with Example in Eclipse
- By Viral Patel on December 28, 2009
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 Tutorials
- Part 1: Introduction to Struts 2
- Part 2: Create Hello World Application in Struts 2
- Part 3: Struts 2 Validation Framework Tutorial with Example
- Part 4: Struts 2 Tiles Plugin Tutorial with Example
- Part 5: Struts 2 Interceptors Tutorial with Example
- Part 6: Struts 2 File Upload and Save Example
- Part 7: Struts 2 Ajax 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.

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.

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.

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

Customer Page with Tiles

Customer Success Page with 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.
Get our Articles via Email. Enter your email address.
Congratulations on this tutorial, I would like to make two comments:
1) I downloaded the latest version of Struts2 and comes with the library “struts2-core-2.1.8.1.jar”. With this library do not work the examples. You have to install the versions “struts2-core-2.0.14.jar” you include the link in part 1 of tutorial that really works well. I have not tested with the version “struts2-core-2.1.6.jar”.
2) I have a problem with Part 4 of the tutorial. I do not understand that serving the “customer-form” as well when you click on the link “customer” an error “HTTP Status 404 – / StrutsHelloWorld / Customer-form” ocurs. I have reviewed the signs of the tutorial and I can not see my mistake.
Thanks in advance
@All: To fix error 404, please correct file Menu.jsp. Add href = “customer-form.action”.
Customer
Hi Viral Patel ,
Thanks for the tutorial,
It is very helpful ,
Iam having below queries on Tiles
Is it possible to use struts tiles like Iframe/Div ?
ie. can i call different URLs in header and footer in above example?
Kindly reply,
Hi Viral,
Nice tutorials ..!
But in the Tiles tutorial, I’m facing the same problem, which was mentioned above by Pedro.
error:
———————————————-
HTTP Status 404 – /mystuff/customer-form
type Status report
message /mystuff/customer-form
description The requested resource (/mystuff/customer-form) is not available.
———————————————-
And, I’m are eagerly waiting for your “Struts 2 Ajax Tutorial with Example”
Kindly reply viral
Hey Viral
Thanks for the tutorials. Though I was stuck in running the hello world but I managed to get it through. Also is there any forums or any thing in which I can ask you about some concepts & meanings of the syntax?? You understand what I am saying??
Regards
Sadia
Hi Viral,
great tutorial! Thank you very much for your work! BTW, i had to modyfiy the Customer.jsp and Welcome.jsp Files to get them displayed correctly with Tiles – if BaseLayout.jsp contains the complete XHTML framework, the “content tiles” should only contain the content of the body tags, i suppose? But maybe this is due to my usage of a css/div-based layout? In addition, i had to change the “add customer” link in Welcome.jsp so it points to customer-form instead of Customer.jsp – otherwise i loose my Tiles-Layout.
Regards
florian
I am having problems. when I click on the customer or add customer button then it shows the customer.action without the tiles & not in the way its show in the picture. then when I press add customer then it shows in the body tile???what could be the problem?
I got the error
———————————————-
HTTP Status 404 – /mystuff/customer-form
type Status report
message /mystuff/customer-form
description The requested resource (/mystuff/customer-form) is not available.
———————————————-
Can anyone share how to fix it?
NK
Hi,
This is a nice tutorial, I appreciate your help.
It’s working fine for me, just don’t know why it’s not taking the whole page, I only see it as a small square in the top center of the page.
thanks
For those that are still trying to fix the 404 on customer-form
in Welcome.jsp and Menu.jsp the href should now point to “customer-form.action” not “customer-form”
Hope this helps
I have downloaded the example and added the Jar, but when i run it on server, it is giving error “The requested resource (/StrutsHelloWorld/) is not available.”, I dont know where i am missing, can u please guide me as i understand everything but i want to chek it out in practical . Thanks in advance,
Good article. Very helpful
Hi. Nice job on the tutorial. I’ve been doing well up until this point. I’ve completed this tutorial but when I try to run I get the following error. Please help. Thanks in advance.
May 21, 2010 3:00:45 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NoSuchMethodError: com.opensymphony.xwork2.util.ValueStack.findValue(Ljava/lang/String;Z)Ljava/lang/Object;
at org.apache.struts2.components.Component.findValue(Component.java:255)
at org.apache.struts2.components.ActionError.evaluateExtraParams(ActionError.java:75)
at org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:856)
at org.apache.struts2.components.UIBean.end(UIBean.java:510)
at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
at org.apache.jsp.Login_jsp._jspx_meth_s_005factionerror_005f0(Login_jsp.java:109)
at org.apache.jsp.Login_jsp._jspService(Login_jsp.java:79)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:389)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
May 21, 2010 3:01:03 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NoSuchMethodError: com.opensymphony.xwork2.util.ValueStack.findValue(Ljava/lang/String;Z)Ljava/lang/Object;
at org.apache.struts2.components.Component.findValue(Component.java:255)
at org.apache.struts2.components.ActionError.evaluateExtraParams(ActionError.java:75)
at org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:856)
at org.apache.struts2.components.UIBean.end(UIBean.java:510)
at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
at org.apache.jsp.Login_jsp._jspx_meth_s_005factionerror_005f0(Login_jsp.java:109)
at org.apache.jsp.Login_jsp._jspService(Login_jsp.java:79)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:389)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
hi struts 2 tiles example is very good . But i want to remove the borders from the tiles and not scroll every time page has to fit for the window . How to do this one usig struts 2 tiles
Kindly suggest me
Thanks in advance
I am having problems. when I click on the customer or add customer button then it shows the customer.action without the tiles & not in the way its show in the picture. then when I press add customer then it shows in the body tile???what could be the problem?
Hi,
I have configured all the steps given in tutorial, Everthing works fine. but in console i seeing warnings i.e,
1. WARNING: Could not find property [org.apache.tiles.AttributeContext.STACK]
2. SEVERE: Could not find action or result
There is no Action mapped for namespace / and action name white. – [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:177)
Can any one help in sort out this issue, Thanks in advance
can i put tiles.xml on resources folder, instead of /WEB-INF/ on eclipse?
Why below configuration is required while use of tiles framework.
if I am setting this value to true my application is not working. Thanks in advance.
Viral,
Another great tutorial. Looking ahead, do you have any plans of creating a struts2-MySQL (using a properties file that encrypts the password) tutorial?
Thanks again for putting this series together,
David Jensen
@David – Thanks for the kind words. I am glad this helped you. Sure, I will try to write more on Struts2 / MySQL and encrypted passwords.
Change “customer-form” to customer-form.action for avoiding / StrutsHelloWorld / Customer-form” error.. hope this will help
Good Day!
First. this tutorial is great you have my thanks
Second.
I have the 404 in BaseLayout. jsp:
HTTP 404 – /StrutsExample/BaseLayout.jsp dont found
I change the menu: for
bur I still dont get it.
Can anyone help me?
Hi VIral,
Thanks the for the nice tutorial. In the tiles tutorial, when I click on the customer link on the welcome page, i get the following error. I tried to import the sample code from the site and still see the error. Any idea why?
HTTP Status 404 – /StrutsHelloWorld/customer-form
type Status report
message /StrutsHelloWorld/customer-form
description The requested resource (/StrutsHelloWorld/customer-form) is not available.
Hi Viral,
This is nice tutorial, but when I tried to run this example, it gives following error.
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 35 in the generated java file
The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:423)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:317)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:282)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:413)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
But, if I remove the jars and configurations for tiles, it runs smoothly.
Any idea… why it is happening…???
Thanks in advance..!!!
I’m getting the following Warning when activating the “Customer” link in the menu:
aug 15, 2011 4:54:18 PM com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
WARNING: No configuration found for the specified action: ‘customer.action’ in namespace: ‘/’. Form action defaulting to ‘action’ attribute’s literal value.
The same happens when I use the files provided at the end of this part of the tutorial.
I have searched google for an answer, but it didn’t turn up anything useful.
Any idea what might be wrong?
Using:
Eclipse Java EE, Indigo, Build id: 20110615-0604
apache-tomcat-7.0.19
jakarta-taglibs-standard-1.1.2
struts-2.2.3
It turned out the error was a typo on my part in the struts.xml file. *blush*
The config-browser, available with struts2, helped me a lot when viewing and resolving the namespace issues.
http://devtalks.blogspot.com/2009/10/struts2-config-browser-plugin.html
While running this application, got below exception
exception
javax.servlet.ServletException: org.apache.tiles.impl.CannotRenderException: ServletException including path ‘/BaseLayout.jsp’.
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:422)
To resolve this we have to update tiles jar files to version 2.2.0
Heya folks,
First of all, thank you for you excellent tutorial. Thank to you, I have saved tons of time and troubles.
I have just a question, maybe because im a newbie with all this stuff and I am not good at it, yet. Well: why should I put in the adress: http://……/customer in order to see the BaseLayout.jsp page? At the start, i tried to acces to this .jsp directly, and then I see that i needed to put just “customer”. By acceding to the BaseLayout.jsp directly I got some errors (404).
I have also “SEVERE: Error executing tag: Attribute ‘header’ not found.” at the start of Apache Tomcat. Maybe some suggestion with that?
Thank you.
Heya,
Thank you again for your great tutorial. Everything is going fine, but I ve some errors while starting Tomcat Server:
java.lang.NoClassDefFoundError: org/apache/tiles/web/startup/TilesListener
How it comes that. My web.xml and the others .xml config files seems be ok. Like that, Tomcat is unable to start /StrutsHelloWorld context, due to previous errors, and I need to put addresses manually like that.
Can you help me, please?
Thanks.
Hi José,
It seems that tomcat couldn’t find tiles-core-2.0.6.jar jar file in your web projects WEB-INF/lib directory. Download this jar file from here and put it in your project’s classpath.
Hello Viral,
Thank you for your fast reply. Well, i had tiles-core-2.2.2.jar instead tiles-core-2.0.6.jar, because with this last one, i had this other error:
Exception starting filter struts2
java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor
I’m really lost, because even if the application works putting manually the addresses, I am not able to start properly tomcat once and for all. Still taking errors since I started to use tiles on my project, and I cannot fix the problem. sigh
Thank you for your help.
Ok, I struggled a few hours to figure out why I got different errors. It looks like you have to have the exact same version of all those jars even for ongl-2.6.11.jar (ongl-3.0.jar does not work).
Hi,
when i am trying above tiles example, i am getting error like below
SEVERE: Error configuring application listener of class org.apache.struts2.tiles.StrutsTilesListener
java.lang.ClassNotFoundException: org.apache.struts2.tiles.StrutsTilesListener
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
SEVERE: Context [/HelloWorld] startup failed due to previous errors
Please help me.
advance thanks.
@SatyamReddy: The ClassNotFoundException occurs if required Struts jar files are not present in your project’s classpath. Please see if all required jar files are in your classpath.
I am getting a strange kind of error. If I include this listener class,
org.apache.struts2.tiles.StrutsTilesListener
be whatever it might be, I get a HTTP status 404 error. I wonder how people are able to get the output? I found this very similar type of discussion in other popular website, but I didn’t find any solution.
Please can anyone clarify it.
Then, after I removed that listener class, I get the welcome screen. But the moment, I give admin and admin123, I am getting a NullPointerException. The log is not anywhere useful I feel because it is not pointing to any part of the program. What I feel is that it is not getting mapped from struts.xml to tiles.xml file.
Please reply as soon as possible.