Apache Tiles is a templating framework built to simplify the development of web application user interfaces.Tiles allows authors to define page fragments which can be assembled into a complete page at runtime.These fragments, or tiles, can be used as simple includes in order to reduce the duplication of common page elements or embedded within other tiles to develop a series of reusable templates.
Lets build a sample tiles app
Configuraion using web.xml
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
<context-param>
<param-name>tiles-definitions</param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</context-param>
Also the configuration can be done using spring xml as follows
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
Define a tiles.xml and place it under WEB-INF
tiles.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://jakarta.apache.org/struts/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="main" template="/layouts/template.jsp">
<put-attribute name="logo" value="/tiles/logo.jsp" />
<put-attribute name="navigation" value="/tiles/navigation.jsp" />
<put-attribute name="body" value="/tiles/indexBody.jsp" />
<put-attribute name="footer" value="/tiles/footer.jsp" />
</definition>
<definition name="offer" template="/layouts/template.jsp" extends="main">
<put-attribute name="body" value="/tiles/offerBody.jsp" />
</definition>
</tiles-definitions>
Tiles also helps in inheritance as you can see
The offer tile definition extends the main definition and can override any of parts of the main definition.
The template looks as follows and can be changed for look and feel
template.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Discount Sale</title>
</head>
<body>
<div id="main">
<tiles:insertAttribute name="logo"/>
<tiles:insertAttribute name="navigation"/>
<tiles:insertAttribute name="body"/>
<tiles:insertAttribute name="footer"/>
</div>
</body>
</html>
All other jsps like logo.jsp , navigation.jsp and footer.jsp can have the logic for view as in any othet jsps.
Saturday, April 11, 2009
Sample Tiles Application
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment