Using Templates

Templates are JSP files that include parameterized content; for instance, A JSP Template (chapterTemplate.jsp) lists a template that can be used to produce web pages with the format shown in A Web Page Layout.

A JSP Template (chapterTemplate.jsp)

<%@ taglib uri='/WEB-INF/tlds/template.tld' prefix='template' %>

 

<html><head><title><template:get name='title'/></title></head>

<body background='graphics/blueAndWhiteBackground.gif'>

 

<table width='610'>

<tr valign='top'><td><template:get name='sidebar'/></td>

<td><table>

<tr><td><template:get name='header'/></td></tr>

<tr><td><template:get name='content'/></td></tr>

<tr><td><template:get name='footer'/></td></tr>

</table>

</td>

</tr>

</table>

</body></html>

template:get is a custom tag that retrieves a Java bean from request scope. That bean contains the URI of a Web component which is subsequently included by template:get. In the template listed in A JSP Template (chapterTemplate.jsp), a bean named header is retrieved from request scope, and the URI--/header.html--is obtained from the bean and included.

Beans retrieved by template:get are put in request scope by template:put, and the template itself is included with template:insert, as illustrated in A JSP Page That Uses theTemplate listed in Example 5-3.a.

A JSP Page That Uses theTemplate listed in A JSP Template (chapterTemplate.jsp)

<%@ taglib uri='/WEB-INF/tlds/template.tld' prefix='template' %>

 

<template:insert template='/chapterTemplate.jsp'>

<template:put name='title' content='Templates' direct='true'/>

<template:put name='header' content='/header.html' />

<template:put name='sidebar' content='/sidebar.jsp' />

<template:put name='content' content='/introduction.jsp'/>

<template:put name='footer' content='/footer.jsp' />

</template:insert>

The template:insert start tag specifies the template to be included, in this case chapterTemplate.jsp, listed in A JSP Template (chapterTemplate.jsp). Each template:put tag stores a bean in request scope and the template:insert end tag includes the template.

A direct attribute can be specified for template:put; if the attribute is set to true, the content associated with the tag is not included, but is printed directly to the implicit out variable. For example, in A JSP Page That Uses theTemplate listed in Example 5-3.a the title content--'JSP Templates'--is used as the window title.

A web site that has many pages with an identical format would have one template, such as the one listed in A JSP Template (chapterTemplate.jsp), and many JSP pages, such as A JSP Page That Uses theTemplate listed in Example 5-3.a, that use the template. If the format is modified, changes are restricted to the template.

Another benefit of templates, and including content in general, is modular design. For example, the JSP file listed in A JSP Page That Uses theTemplate listed in Example 5-3.a ultimately includes header.html, which is listed in header.html.

header.html

<table>

<tr>

<td><img src='graphics/java.gif'/></td>

<td><img src='graphics/layout.gif'/></td>

</tr>

</table>

<hr>

Although header.html is an HTML file, it does not contain the usual preamble of tags such as <html>, <body>, etc. because those tags are in the template. Because the template takes care of such housekeeping, included files such as header.html are simpler and less prone to errors.

 
JSP Tip
When To Use Templates

Templates provide a mechanism to encapsulate web page layout. Because layout is specified in a single template and used for many web pages, the impact of layout changes is reduced to the template itself.

Because the main benefit of templates is reducing the impact of layout changes, templates are most applicable to large websites that have many web pages with identical formats.