JSP Template

JSP templates minimize the impact of layout changes and encourage modular design.

Although Web development tools are rapidly progressing, they still lag behind most graphical user interface (GUI) toolkits such as Swing or VisualWorks Smalltalk. For example, traditional GUI toolkits provide layout managers, in one form or another, that allow layout algorithms to be encapsulated and reused. This article explores a template mechanism for JavaServer Pages (JSP) that, like layout managers, encapsulates layout so it can be reused instead of replicated.

Because layout undergoes many changes over the course of development, it's important to encapsulate that functionality so it can be modified with minimal impact to the rest of the application. In fact, layout managers demonstrate an example of one of the tenets of object-oriented design: encapsulate the concept that varies, which is also a fundamental theme for many design patterns.

JSP does not provide direct support for encapsulating layout, so Webpages with identical formats usually replicate layout code (header, footer, sidebar, etc)

Example 1: A template
<html><head><title>JSP Templates</title></head>
<body background='graphics/background.jpg'>

<table>
   <tr valign='top'><td><%@include file='sidebar.html'%></td>
      <td><table>
         <tr><td><%@include file='header.html'%></td></tr>
         <tr><td><%@include file='introduction.html'%></td></tr>
         <tr><td><%@include file='footer.html'%></td></tr>
         </table>
      </td>
   </tr>
</table>
</body></html>

In the example listed above, content is included with the JSP include directive, which allows the content of the page to vary — by changing the included files — without modifying the page itself. However, because layout is hard coded, layout changes require modifications to the page. If a Website has multiple pages with identical formats, which is common, even simple layout changes require modifications to all of the pages.

To minimize the impact of layout changes, we need a mechanism for including layout in addition to content; that way, both layout and content can vary without modifying files that use them. That mechanism is JSP templates.

Templates are JSP files that include parameterized content.

Example 2.a: A template
<%@ taglib uri='/WEB-INF/tlds/template.tld' prefix='template' %>

<html><head><title><template:get name='title'/></title></head>
<body background='graphics/background.jpg'>

<table>
   <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>

Example 2.a is nearly identical to Example 1, except we use template:get instead of the include directive.

template:get retrieves a Java bean with the specified name from request scope. The bean contains the URI (Uniform Resource Identifier) of a Web component that's included by template:get. For example, in the template listed in Example 2.a, template:get obtains a URI — header.html — from a bean named header in request scope. Subsequently, template:get includes header.html.

Example 2.b. Using the template from Example 2.a (illustrates the use of the put and insert tags)
<%@ taglib uri='/WEB-INF/tlds/template.tld' prefix='template' %>

<template:insert template='/articleTemplate.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.html'/>
  <template:put name='footer' content='/footer.html' />
</template:insert>

template:put puts the beans in request scope that are subsequently retrieved by template:get. The template is included with template:insert.

The insert start tag specifies the template to be included, in this case the template listed in Example 2.a. Each put tag stores a bean in request scope and the insert end tag includes the template.

A direct attribute can be specified for template:put. If direct is set to true, the content associated with the tag is not included by template:get, but is printed directly to the implicit out variable.

Websites containing multiple pages with identical formats have one template, such as the one listed in Example 2.a, and many JSP pages, such as Example 2.b, that use the template. If the format is modified, changes are restricted to the template.

JSP provides two ways to include content: statically, with the include directive, and dynamically, with the include action. The include directive includes the source of the target page at compile time and is equivalent to C's #include or Java's import. The include action includes the target's response generated at runtime.

Like the JSP include action, templates include content dynamically.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License