Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Developing Simple Struts Tiles Application 
 

In this tutorial I will show you how to develop simple Struts Tiles Application. You will learn how to setup the Struts Tiles and create example page with it.

 

Developing Simple Struts Tiles Application

                         

Introduction
In this section  I will show you how to develop simple Struts Tiles Application. You will learn how to setup the Struts Tiles and create example page with it.

What is Struts Tiles?
Tiles is a framework for the development user interface. Tiles is enables the developers to develop the web applications by assembling the reusable tiles (jsp, html, etc..). Tiles uses the concept of reuse and enables the developers to define a template for the web site and then use this layout to populate the content of the web site. For example, if you have to develop a web site having more that 500 page of static content and many dynamically generated pages. The layout of the web site often changes according to the business requirement. In this case you can use the Tiles framework to design the template for the web site and use this template to populate the contents. In future if there is any requirement of site layout change then you have to change the layout in one page. This will change the layout of you whole web site.

Steps To Create Tiles Application
Tiles is very useful framework for the development of web applications. Here are the steps necessary for adding Tiles to your Struts application:

  1. Add the Tiles Tag Library Descriptor (TLD) file to the web.xml.
  2. Create layout JSPs.
  3. Develop the web pages using layouts.
  4. Repackage, run and test application.

Add the Tiles TLD to web.xml file
Tiles can can be used with or without Struts. Following entry is required in the web.xml file before you can use the tiles tags in your application.

<taglib>
     <taglib-uri>/tags/struts-tiles</taglib-uri>
      <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>

   
Create layout JSPs.

Our web application layout is divided into four parts: To Banner, Left Navigation Bar, Content Area and Bottom of the page for copy right information. Here is the code for out template (template.jsp):

<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<html>

<head>
    <title><tiles:getAsString name="title" ignore="true"/></title>
</head>

<body>

<table border="1" cellpadding="0" cellspacing="0" width="100%" bordercolor="#000000" bgcolor="#E7FDFE">
<tr>
<td width="100%" colspan="2" valign="top"><tiles:insert attribute="header"/></td>
</tr>
<tr>
<td width="23%"><tiles:insert attribute="menu"/></td>
<td width="77%" valign="top" valign="top"><tiles:insert attribute="body"/></td>
</tr>
<tr>
<td width="100%" colspan="2" valign="top"><tiles:insert attribute="bottom"/></td>
</tr>
</table>

</body>

</html>

We have defined the structure for web application using the appropriate html and did the following things:

  • Referenced the /WEB-INF/struts-tiles.tld TLD.
  • Used the string parameters to display title using the tiles:getAsString tag. If the attribute ignore="true" then Tiles ignore the missing parameter. If this is true then the Tiles framework will through the exception in case the parameter is missing.
  • To insert the content JSP, the tiles:insert tag is used, which inserts any page or web resources that framework refers to as a title. For Example <tiles:insert attribute="header"/> inserts the header web page.

Develop the web pages using layouts
Now we will use tile layout create a page to display the content page in the in our application. For every content page there is additional jsp file for inserting the content in the Layout, so we have to create two jsp files one for content and another for displaying the content. In our example these file are example.jsp and content.jsp. Here is the code for both the files:

content.jsp

<p align="left"><font color="#000080" size="5">Welcome to the Title Tutorial</font></p>
<p align="left"><font color="#000080" size="5">This is the content page</font></p>

The content.jsp simply define the content of the page. The content may be dynamic or static depending on the requirements.

example.jsp

<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>

<tiles:insert page="/tiles/template.jsp" flush="true">
   <tiles:put name="title" type="string" value="Welcome" />
   <tiles:put name="header" value="/tiles/top.jsp" />
   <tiles:put name="menu" value="/tiles/left.jsp" />
   <tiles:put name="body" value="/tiles/content.jsp" />
   <tiles:put name="bottom" value="/tiles/bottom.jsp" /> 
</tiles:insert>

The code <tiles:insert page="/tiles/template.jsp" flush="true"> specifies the tiles layout page to be used. We have set the flush attribute to true, this makes the tile file to be written to browser before the rest of the page. To specify the title of the page <tiles:put name="title" type="string" value="Welcome" /> is used. The following code is used to insert the actual pages in the template.:

   <tiles:put name="header" value="/tiles/top.jsp" />
   <tiles:put name="menu" value="/tiles/left.jsp" />
   <tiles:put name="body" value="/tiles/content.jsp" />
   <tiles:put name="bottom" value="/tiles/bottom.jsp" /> 

The top.jsp will be inserted in the layout's header region. The left.jsp will be inserted in the layout's menu region. The content.jsp wil be inserted in the layout's body region and the bottom.jsp will be inserted in the bottom region.

Repackage, run and test application
Add the following code in the index.jsp to test the this tile example:

<li>
<html:link page="/tiles/example.jsp">Tiles Example</html:link>
<br>
Example of creating first tile application.
</li>

Use the ant tool to build the application and deploy on the server. To test the application go to the index.jps and click on the Tiles Example link.

                         

» View all related tutorials
Related Tags: c programming apache web com development asp ui framework build application io components make sed get ip hook vi concepts

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

67 comments so far (
post your own) View All Comments Latest 10 Comments:

Hi this is lohith
i am using struts1.1
and tiles1.1

in jsp pagae it is showing the error
tag.getAsString : component context is not defined

if i remove title

it is showing
java.lang.NullPointerException
org.apache.struts.taglib.tiles.InsertTag.processAttribute(InsertTag.java:689)

how can it be solved

with regards
Lohith

Posted by lohith on Sunday, 12.21.08 @ 23:31pm | #83023

Hi this is lohith
i have using tiles1.1
at the time of execution at this time showing
http 500 error
Error is in jsp is
type Exception report

message

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

exception

java.lang.IllegalArgumentException: Path Tiles.Example does not start with a "/" character
i dont now the error
plz help,

With Regards
Lohith

Posted by lohith on Sunday, 12.21.08 @ 23:01pm | #83020

ladies and gentlement,

I got the trouble, when I implement my web site. I want to implement tiles via servlet, but I didn't know, where I should put the uri, and how?

please help me..

thanks

Posted by Sarwo Edi Wibowo on Tuesday, 12.16.08 @ 02:45am | #82810

you have to add the struts capabilities if you are using myeclipse IDE then automatically struts-tiles.tld file will be created

Posted by Deepak Gupta on Wednesday, 12.3.08 @ 03:09am | #82310

I use struts 2 but i don't see the file struts-tiles.tld in folder WEB-INF so when i insert code
<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
in to web.xml
When i run i show error.
How to solve this problem

Posted by hoa phạm on Monday, 10.13.08 @ 07:49am | #81034

I am having the same prb..did u get some solution for the same.if yes plz do tell me also..thanks

Posted by Priya on Tuesday, 08.12.08 @ 16:07pm | #72505

it's very good and need to be covered in detail with other tags of the tiles framework, thankyou

Posted by kishore on Thursday, 08.7.08 @ 12:24pm | #71536

Hi,
Is there anyone let me know if there is any document guide us how to use strut 2 tiles?

Thanks in advance!

Posted by kienjudo on Thursday, 03.6.08 @ 08:18am | #51652

how can we set the target of menu to body in struts tiles frame work?it is opening in a new page every time?

Posted by G.Venu Prasad on Friday, 02.22.08 @ 17:36pm | #49419

Its very useful and easy for d beginners to learn.....

Posted by balskrishnan on Friday, 02.1.08 @ 17:13pm | #46862

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.