Use templates to standardize layout

Templates are commonly used to enforce a uniform appearance across a set of related pages - often even for an entire web site.

If using JSPs, templates can be easily implemented using either the <@include> directive or the <jsp:include> action.

Example

Here, a template JSP defines the layout. It includes a header, footer, and navigation links. A <jsp:include> inserts the main content or "body" of the page. There are two parameters to the template, which are passed in as request parameters : The <tags:xxx/> items refer to .tag files, that contain small JSP snippets.
<%@ include file="/JspHeader.jsp" %>
<
  !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  "http://www.w3.org/TR/html4/loose.dtd"
> 
<html>
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=uTF-8">
 <%-- TTitle is a request parameter for the templated page title. --%>
 <title>
  Fish and Chips Club - ${param.TTitle} </title>
 <link rel="stylesheet" type="text/css" href="../../stylesheet.css" media="all">
</head>

<body>
<div align="center">
 <img class="no-margin" src="../../images/logo.jpg" alt="Fish And Chips Club">
</div>

<div class="header">
 "The Total Fish & Chips Dining Experience"
</div>

<div class="menu-bar">
 <w:highlightCurrentPage styleClass='highlight'>
  <c:url value="/main/home/HomePageAction.do" var="homeURL"/> 
  <A href='${homeURL}'>Home</a>
  <c:url value="/main/rsvp/RsvpShow.do" var="showRsvpURL"/> 
  <A href='${showRsvpURL}'>Rsvp</a>
  <c:url value="/all/logoff/LogoffAction.do" var="logoffURL"/> 
  <A href='${logoffURL}'>Log Off</a>
 </w:highlightCurrentPage>
</div>

<%-- Display error and information messages. --%>
<tags:displayMessages/>

<%-- TBody is a request parameter for the templated page body. --%>
<div class="body">
 <c:if test="${not empty param.TBody}">
  <jsp:include page='${param.TBody}' flush="true"/>
 </c:if>
 <c:if test="${empty param.TBody}">
  <jsp:include page="Error.jsp" flush="true"/>
 </c:if>
</div>

<tags:footer/>

</body>
</html>


See Also :
Always maintain HttpSessions
Prefer JSTL tags
Prevent self-linking
A Web App Framework - WEB4J
Would you use this technique?
Yes   No   Undecided   
© 2014 Hirondelle Systems | Source Code | Contact | License | RSS
Individual code snippets can be used under this BSD license - Last updated on September 21, 2013.
Over 2,000,000 unique IPs last year - Built with WEB4J.
- In Memoriam : Bill Dirani -