Saturday, October 8, 2011

Using Tiles 2 without Struts

Sometimes we need tiles functionality and don't want to use Struts. Tiles is very useful when we want to use data access by other frameworks like DWR. This blog displays step by step procedure for using Apache Tiles 2 Without Struts.


Step 1: Create a new dynamic web project in eclipse and put the below jar files to WEB-INF/lib folder (you can exclude dwr jar files).




Step 2: Now configure the tiles listener and tiles servlet in web.xml file as below.



<context-param>
<param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG
                </param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</context-param>
<listener>
<listener-class>org.apache.tiles.web.startup.TilesListener</listener-class>
</listener>
<servlet>
<servlet-name>Tiles Dispatch Servlet</servlet-name>
<servlet-class>org.apache.tiles.web.util.TilesDispatchServlet</servlet-class>
</servlet>
<servlet-mapping> 
         <servlet-name>Tiles Dispatch Servlet</servlet-name> 
         <url-pattern>*.tiles</url-pattern> 
</servlet-mapping>

Step 3: Now create a template jsp file like below template.jsp in WebContent folder

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test</title>
</head>
<body style="font-family: verdana;">
<table align="center" border=0 height="100%" width="100%">
<tr height="5%">
<td><tiles:insertAttribute name="header" /></td>
</tr>
<tr height="5%">
<td><tiles:insertAttribute name="menu" /></td>
</tr>
<tr height="75%">
<td><tiles:insertAttribute name="body" /></td>
</tr>
<tr height="5%" >
<td><tiles:insertAttribute name="footer" /></td>
</tr>
</table>
</body>
</html>

Step 4: Create folur other jsp  files like header.jsp, menu.jsp, home.jsp and footer.jsp in WebContent folder

Step 5: Now create a tiles.xml file inside WEB-INF folder and create template and other pages like below

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>
<definition name="outerLayout" template="teplate.jsp">
<put-attribute name="header" value="header.jsp" />
<put-attribute name="menu" value="menu.jsp" />
<put-attribute name="body" value="" />
<put-attribute name="footer" value="footer.jsp" />
</definition>
<definition name="homePage" extends="outerLayout">
<put-attribute name="body" value="home.jsp" />
</definition>
</tiles-definitions>

Step 6: Now start web-server and use the URL like 

http://localhost:8080/TGReality/homePage.tiles

all the pages will be called by using the name of tiles definition like homePage.tiles.

don't change *.tiles in web.xml.




1 comment:

  1. This post is very simple to read and appreciate without leaving any details out. Great work! healthcare call center software

    ReplyDelete