Try it here
JSP templates

Use JSP templates to encapsulate Webpage layout


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; for example, Figure 1 shows a Webpage containing header, footer, sidebar, and main content sections.

$IMG The layout of the page shown in Figure 1 is implemented with HTML table tags:

Example 1. Including content

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

Using templates

Templates are JSP files that include parameterized content. The templates discussed in this article are implemented with a set of custom tags: template:get, template:put, and template:insert. The template:get tag accesses parameterized content, as illustrated in Example 2.a, which produces Webpages with the format shown in Figure 1.

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. Let's examine how template:get works.

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.

template:put puts the beans in request scope that are subsequently retrieved by template:get. The template is included with template:insert. Example 2.b illustrates the use of the put and insert tags:

Example 2.b. Using the template from Example 2.a

<%@ 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>

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. The template subsequently accesses the beans as described above.

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. In Example 2.b, for example, the title content -- JSP Templates -- is used for the window title.

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.

Another benefit of templates and including content in general is modular design. For example, the JSP file listed in Example 2.b ultimately includes header.html, listed in Example 2.c.

Example 2.c. header.html

<table> <tr> <td><img src='graphics/java.jpg'/></td> <td><img src='graphics/templates.jpg'/></td> </tr> </table><hr>

Because header.html is included content, it does not have to be replicated among pages that display a header. Also, although header.html is an HTML file, it does not contain the usual preamble of HTML tags such as or because those tags are defined by the template. That is, because the template includes header.html, those tags should not be repeated in header.html.

Note: 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. So, although the JSP pages in Example 1 and Example 2.b are functionally identical, the former statically includes content, whereas the latter dynamically includes it.

Writer profile pic

Uk01 on Dec 11, 2014 at 12:12 am

If you like and would like to contribute, you can write your article here or mail your article to . See your article appearing on the main page and help others to learn.

Post Comment

Comments( 0)


Forgot Password

Please enter your email address below and we will send you information to change your password.