More On Tiles

The purpose of tiles is to have a common layout for all pages in an application (or module). All pages should have a similar look and feel with respect to position of header/ footer/ sidebar/ title components.
Also it encourages reuse of component JSPs that are inserted into multiple pages. For example there is a separate header.jsp defined that is inserted into all pages of the application at runtime. This makes more it more reusable than copying/ pasting the contents of the footer JSP into each of the pages.
Another reason for using tiles is to give pseudo names for JSP pages so that JSPs are not referred to by their physical names.
In the stuts-config.xml add this entry (if a similar entry exists, modify it to look like this)

<!—Tiles controller-->
        <controller processorClass=“org.apache.struts.tiles.TilesRequestProcessor”
	      nocache=“true” />
<!-- Plug Ins Configuration-->
        <plug-in className = “org.apache.struts.tiles.TilesPlugin”>
	      <set-property property = “definitionConfigFiles” value = “/WEB-INF/tiles-defs.xml/>
	</plug-in>

This directive instructs struts to use Tiles (It’s an optional thing).
In tiles, there is a “Layout” JSP (which is the main JSP) which defines how the page should look like, that is, defines the positions of the footer, header, sidebar, title, etc.

<!—DOCTYPE HTML PUBLIC = “-//W3C//DTD HTML 4.01 Transitional//EN”>
<%@ taglib uri=“/WEB_INF/tiles.tld” prefix=“tiles”%>
<%@ taglib uri = “/WEB_INF/struts-html.tld prefix = “html”%>
<html>
<head>
<title><tiles:getAsString name=“title”>
</title>
<!—this means that title is a string that goes here and the value will be specified in tiles-defs.xml-->
</head>
<tiles: insert attribute=“header” />
<tiles: insert attribute=“body”
	<!—this means that body is a jsp that goes here and the jsp name will be specified in tiles-defs.xml-->

</html>

Here as you can see, the layout defines that the title is a string that appears in the

<head>

tag and that the header and body are JSPs that get inserted after that.
In addition you can define more inserts for other JSPs that constitute the footer, sidebar, etc.
The /WEB-INF/tiles-defs.xml file will define the layout pages and individual JSP pages used by the application.

It looks like this

<!DOCTYPE tiles-definitions PUBLIC “//Apache Software Foundation//DTD Tiles Configuration//EN” http://jakarta.apache.org/struts/dtds/tiles-config.dtd>
<tiles-definitions> <!—Master Template-->
	<definition name=“layout” path=“/WEB-INF/views/layout/Layout.jsp”>
		<put name=“title” value=“” />
	</definition> <!—Page Definitions-->
	<definition name=“searchPage” extends=“layout”>
		<put name=“body” value=“/views/body/search/SearchForm.jsp” />
		<put name=“title” value=“Search” />
		< put name = header” value =
		“/WEB-INF/views/layout/Header.jsp”>
	</definition>
	<definition name=“displayPage” extends=“layout”>
		<put name=“body” value=“/views/body/display/DisplayForm.jsp” />
		<put name=“title” value=“Display” />
		<put name=“header” value=“/WEB-INF/views/layout/Header.jsp” />
	</definition> <!—similarly more page definition-->
<!—all these pages use the same layout that defines position of title and body. The pages use a different body but the same header-->
</tiles-definitions>

struts-config.xml
In the action mappings, instead of mentioning the names of the JSPs directly, use the pseudo names, specified in tiles-defs.xml.
eg. Use searchPage and displayPage instead of using the JSP file names “/views/body/search/SearchForm.jsp and /views/body/display/DisplayForm.jsp.

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Comments are closed.

%d bloggers like this: