Tutorial: Struts Tiles plugin example in Eclipse

In the previous tutorials about Creating Struts application in Eclipse, we saw step by step how we can create a Struts project. In this tutorial we will explore Tiles plugin in struts.

Introduction of Tiles Plugin

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.

Installing Tiles

The Tiles installation process depends on the Struts version you use. If you start a new project,
use the latest Struts version. Tiles can also be used without Struts.

Add following entry of plug-in tag to your struts-config.xml in order to configure Tiles.

<plug-in className="org.apache.struts.tiles.TilesPlugin">
	<set-property property="definitions-config"
		value="/WEB-INF/tiles-definitions.xml" />
	<set-property property="moduleAware" value="true" />
</plug-in>

In this entry you can see definitions-config parameter which is set to the value /WEB-INF/tiles-definitions.xml. So we have to create a file called tiles-definitions.xml in WEB-INF directory.

Creating Application

We will create a Header and a Footer that we will integrate with our sturts application. We will render this header and footer using Tiles.
Create two JSPs, Header.jsp and Footer.jsp and copy following content in it.

Header.jsp

<div style="width: 100%; height: 200px; background-color: #ABBAF3">
	<h3>Tiles Plugin Example using Struts &amp;amp;amp;amp; Eclipse</h3>
</div>

Footer.jsp

<div style="width: 100%; height: 50px; background-color: #ABBAF3">
	Copyright &amp;amp;amp;amp;copy viralpatel.net
</div>

Also, create a Layout.jsp file in your WebContent folder and copy following code in it.

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles"%>
<tiles:importAttribute />
<HTML>
<HEAD>
		<TITLE><tiles:getAsString name="title" /></TITLE>
</HEAD>
<BODY>
<TABLE width="100%" height="100%" border="0">	
	<TR>
		<TD valign="top" height="10px">
			<tiles:insert name="header" attribute="header" />
		</TD>
	</TR>
	<TR>
		<TD  valign="middle" align="center">
			<tiles:insert name="body" attribute="body" />
		</TD>
	</TR>		
	<TR>
		<TD height="10px">
			<tiles:insert name="footer" attribute="footer" />
		</TD>
	</TR>
</TABLE>
</BODY>
</HTML>

Layout.jsp file will define overall layout of web page. I have used a table based layout here. You can use a DIV based layout and align all the components using CSS.
Note that we have used a tag <tile:insert> in order to place the respective elements on this page. This will be more clear once you create tiles-definitions.xml file.

Now copy following code in tiles-plugins.xml file.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 1.3//EN"
       "http://struts.apache.org/dtds/tiles-config_1_3.dtd">

<tiles-definitions>
	<definition name="layout" page="/Layout.jsp">
		<put name="header" value="/Header.jsp" />
		<put name="body" value="" />
		<put name="footer" value="/Footer.jsp" />
	</definition>

	<definition name="/tiles.home" extends="layout">
		<put name="title" value="Welcome: Tiles Plugin Sturts Tutorial" />
		<put name="body" value="/wome.jsp" />
	</definition>
</tiles-definitions>

Here we have replace names like title, body, header, footer etc with the content of the JSPs. Hence tiles will replace the tag <tile:insert> with appropriate content in Layout.jsp.



21 Comments

  • Rakesh 19 December, 2008, 13:51

    Good work… brother..its good for all begginer.. plz.. if possible give Hibernate tutorial in eclipse..plz.. plz..pz…

  • Viral 19 December, 2008, 14:07

    Thanks Rakesh for the comment..
    I will sure try to put some step by step tutorials on Hibernate and other ORMs on this site.

  • richa 23 February, 2009, 17:47

    you have not provided sufficient codes. Nobody can run the application using your codes.

  • Viral Patel 23 February, 2009, 18:12

    Hi Richa, I have provided full code and with this you must be able to run the application. Let me know exactly where you are getting the problem.
    Have you created basic struts application? I have kept source code in the tutorial: http://viralpatel.net/blogs/tutorial-creating-struts-application-in-eclipse/

  • Juny Fan 6 March, 2009, 2:55

    0

  • smita 31 December, 2009, 11:17

    Hi viral,
    I am also getting the same exception .ie javax.servlet.ServletException: Error – tag importAttribute : no tiles context found.
    Please guide me to resolve this.

  • Anshul 12 March, 2010, 23:32

    HI! im getting this error cud u plz help me……….
    HTTP Status 500 –

    ——————————————————————————–

    type Exception report

    message

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    java.lang.NullPointerException
    org.apache.struts.tiles.xmlDefinition.FactorySet.getDefinition(FactorySet.java:116)
    org.apache.struts.tiles.definition.ComponentDefinitionsFactoryWrapper.getDefinition(ComponentDefinitionsFactoryWrapper.java:87)
    org.apache.struts.tiles.TilesRequestProcessor.processTilesDefinition(TilesRequestProcessor.java:184)
    org.apache.struts.tiles.TilesRequestProcessor.processForwardConfig(TilesRequestProcessor.java:332)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:232)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803

  • Sudheer 20 June, 2010, 20:09

    In fact for any info im visiting u r site, very good site..helping lot

  • Kosha 21 March, 2011, 17:05

    I m getting this error PLEASE REPLY!!!!!!!!!!!!!!
    HTTP Status 500 –

    ——————————————————————————–

    type Exception report

    message

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    org.apache.jasper.JasperException: An exception occurred processing JSP page /index3.jsp at line 3

    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
    root cause

    javax.servlet.ServletException: javax.servlet.jsp.JspException: Can’t get definitions factory from context.
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    root cause
    javax.servlet.jsp.JspException: Can’t get definitions factory from context.
    org.apache.struts.tiles.taglib.InsertTag.processDefinitionName(InsertTag.java:580)
    org.apache.struts.tiles.taglib.InsertTag.createTagHandler(InsertTag.java:479)
    org.apache.struts.tiles.taglib.InsertTag.doStartTag(InsertTag.java:441)
    org.apache.jsp.index3_jsp._jspx_meth_tiles_005finsert_005f0(index3_jsp.java:86)
    org.apache.jsp.index3_jsp._jspService(index3_jsp.java:59)

    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.14 logs.

    PLEASE HELP!!!!!!!!!!!!!!!!!!!!!!!!!!

  • Kosha 21 March, 2011, 17:06

    Please show how to use the definition tiles.home that is created

  • Nimesh 21 March, 2011, 17:08

    Please tell how to use the definition tiles.home

  • niraj 16 January, 2012, 12:17

    i m getting NoSuchAttributeException for my tiles with struts 2 intefration projet.
    can y please help me to get rid off this problem…………..

  • Chetan Malabade (Blue Hacker) 10 March, 2012, 20:59

    hi viral…….
    m trying this application, but everytime m getting the following error. when m adding the jar files into the application then i got an following error. but when m removing the jsr files then the application will run without any error….m also changed the jsr files but the same exception will cause……….plz help me. or send me detailed demo of application….plz.. this is very help full for me. …..m waiting..

    Incrementally deploying http://localhost:8084/demo1
    Completed incremental distribution of http://localhost:8084/demo1
    Incrementally redeploying http://localhost:8084/demo1
    Deploy is in progress…
    deploy?config=file%3A%2FC%3A%2FUsers%2FPOWER%2FAppData%2FLocal%2FTemp%2Fcontext48354.xml&path=/demo1
    FAIL – Deployed application at context path /demo1 but context failed to start
    C:\Users\POWER\Documents\NetBeansProjects\demo1\nbproject\build-impl.xml:580: The module has not been deployed.
    BUILD FAILED (total time: 11 seconds)

  • Neel 9 June, 2012, 8:14

    org.apache.struts.tiles.TilesPlugin

    how to get this TilesPluin Class

    • Viral Patel 9 June, 2012, 12:05

      You will need struts-tiles.jar in your classpath.

  • K Padmanaban 17 July, 2012, 14:33

    Hi there,
    I am using NetBeans IDE for a long time for java/j2ee development. Now I am leaning Spring MVC. Where do I get the Spring core packages to be downloaded so that I can do a bit in Spring MVC ? Please help me. Also what are the packages necessry to develop any spring application in Netbeans IDE.

    Thanks and regards.

    K Padmanaban

  • karthic 19 January, 2013, 21:38

    HTTP Status 500 – javax.servlet.ServletException: javax.servlet.jsp.JspException: Error – tag.getAsString : component context is not defined. Check tag syntax

    ——————————————————————————–

    type Exception report

    message javax.servlet.ServletException: javax.servlet.jsp.JspException: Error – tag.getAsString : component context is not defined. Check tag syntax

    description The server encountered an internal error that prevented it from fulfilling this request.

    exception

    org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: Error – tag.getAsString : component context is not defined. Check tag syntax
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:585)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

    root cause

    javax.servlet.ServletException: javax.servlet.jsp.JspException: Error – tag.getAsString : component context is not defined. Check tag syntax
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
    org.apache.jsp.Header_jsp._jspService(Header_jsp.java:91)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

    root cause

    javax.servlet.jsp.JspException: Error – tag.getAsString : component context is not defined. Check tag syntax
    org.apache.struts.tiles.taglib.GetAttributeTag.doEndTag(GetAttributeTag.java:158)
    org.apache.jsp.Header_jsp._jspx_meth_tiles_005fgetAsString_005f0(Header_jsp.java:110)
    org.apache.jsp.Header_jsp._jspService(Header_jsp.java:75)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

    note The full stack trace of the root cause is available in the Apache Tomcat/7.0.33 logs.

    ——————————————————————————–

    Apache Tomcat/7.0.33

  • Yesubabu 30 March, 2013, 20:06

    Nice,Good Job.
    And Thank you.

  • swati 24 April, 2013, 11:52

    want to see web.xml

  • mahendra 5 August, 2013, 20:08

    veryGood Example
    thanks

  • rajesh 1 April, 2014, 10:30

    Here where is web.xml

Leave a Reply

Your email address will not be published. Required fields are marked *

Note

To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]