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 >/WEB-INF/tiles-definitions.xml</strong>. So we have to create a file called tiles-definitions.xml in WEB-INF directory. <img src="//www.viralpatel.net/app/uploads/2008/12/creating-tiles-definition.png" alt="" title="creating-tiles-definition" width="215" height="188" class="alignnone size-full wp-image-351" /> <h2>Creating Application</h2> 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. <strong>Header.jsp</strong> <!-- wp:code {"language": "html"} --><pre class="wp-block-code"><code></code></pre><!-- /wp:code --> <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.

View Comments

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

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

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

  • 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

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

Share
Published by
Viral Patel
Tags: Struts struts-plugin tiles tiles-plugin

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

4 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

4 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

4 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

4 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

4 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

4 years ago