|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
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.
Figure 1. Webpage layout
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.
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 <html> or <body> 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 theincludedirective, and dynamically, with theincludeaction. Theincludedirective includes the source of the target page at compile time and is equivalent to C's#includeor Java'simport. Theincludeaction includes the target's response generated at runtime.
Like the JSP
includeaction, 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.