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.


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" 
 <meta http-equiv="Content-Type" content="text/html; charset=uTF-8">
 <%-- TTitle is a request parameter for the templated page title. --%>
  Fish and Chips Club - ${param.TTitle} </title>
 <link rel="stylesheet" type="text/css" href="../../stylesheet.css" media="all">

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

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

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

<%-- Display error and information messages. --%>

<%-- 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 test="${empty param.TBody}">
  <jsp:include page="Error.jsp" flush="true"/>



See Also :
Always maintain HttpSessions
Prefer JSTL tags
Prevent self-linking
A Web App Framework - WEB4J
Would you use this technique?
Yes   No   Undecided   
© 2015 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 -