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>
Code language: HTML, XML (xml)
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>
Code language: HTML, XML (xml)
Footer.jsp
<div style="width: 100%; height: 50px; background-color: #ABBAF3"> Copyright &amp;amp;amp;amp;copy viralpatel.net </div>
Code language: HTML, XML (xml)
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>
Code language: HTML, XML (xml)
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>
Code language: HTML, XML (xml)
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.
Get our Articles via Email. Enter your email address.

You may also like...

23 Comments

  1. Rakesh says:

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

  2. Viral says:

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

  3. richa says:

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

  4. 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/tutorial-creating-struts-application-in-eclipse/

  5. Juny Fan says:

    0

  6. smita says:

    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.

  7. Anshul says:

    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

  8. Sudheer says:

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

  9. Kosha says:

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

  10. Kosha says:

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

  11. Nimesh says:

    Please tell how to use the definition tiles.home

  12. niraj says:

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

  13. Chetan Malabade (Blue Hacker) says:

    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)

  14. Neel says:

    org.apache.struts.tiles.TilesPlugin

    how to get this TilesPluin Class

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

  15. K Padmanaban says:

    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

  16. karthic says:

    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

  17. Yesubabu says:

    Nice,Good Job.
    And Thank you.

  18. swati says:

    want to see web.xml

  19. mahendra says:

    veryGood Example
    thanks

  20. rajesh says:

    Here where is web.xml

  21. mustapha says:

    Good Job

  22. Prakash says:

    Your Tutorial superb Patel Ji
    Plz provide Screen shots of output…

    Thanks and Regards
    Prakash

Leave a Reply

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