Tutorial: Struts Tiles plugin example in Eclipse
- By Viral Patel on December 8, 2008
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.
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.
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.
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.
Tiles Plugin Example using Struts &amp;amp; Eclipse
Copyright &amp;amp;copy viralpatel.net
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"%>
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
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.
Here we have replace names like title, body, header, footer etc with the content of the JSPs. Hence tiles will replace the tag
with appropriate content in Layout.jsp.
Get our Articles via Email. Enter your email address.