There are many different approaches to the problem, but the open source
Maverick MVC framework offers a pretty elegant solution. Aside from all
the coolness of having an MVC framework (separation of logic and
presentation, etc), Maverick allows you to create pipelines of
"document" transforms.
Document transforms are very simple: A series of documents (jsp,
velocity, whatever) are executed in order. The output from the previous
step is passed to the next step as a String in the request attributes.
Also, parameters can be passed forwards by setting request attributes,
so it's easy for the initial document to specify a title or other
characteristics used in the "outer" look-and-feel templates.
Here's an example of how it works. You can define a command in
Maverick's config file like this (this is a *very* simple example):
<command name="products">
<view path="products.jsp">
<transform path="lookAndFeel.jsp"/>
</view>
</command>
A request for "products.m" will cause this command to be executed. You
can have a products.jsp that looks like this:
<% request.setAttribute("title", "Our Products"); %>
<p>
These are our products blah blah blah...
</p>
And the lookAndFeel.jsp would look like this:
<html>
<head><title> <%= request.getAttribute("title") %> </title></head>
<body>
<p>Stuff for every page blah blah blah</p>
<%= request.getAttribute("wrapped") %>
</body>
</html>
Maverick allows you to use other transformation technologies as well,
like XSLT and DVSL, but for simple templating like you're looking for,
the document transform is pretty convenient.
Take a look: http://mav.sourceforge.net
We're just a few days away from the 2.0 release, but that's just because
we are ironing out the docs. The framework itself is quite stable and
in production use. Maverick has an Apache-style license. It was
inspired by Struts but offers more features (such as transformation and
automagic internationalization) with a much easier to use interface. It
is view-technology agnostic, so you can use Velocity, XSLT (without
generating XML!), JSP (bring your own tag library), or pretty much any
other templating language.
Jeff Schnitzer
[EMAIL PROTECTED]
> -----Original Message-----
> From: Soefara Redzuan [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, February 26, 2002 10:39 PM
> To: [EMAIL PROTECTED]
> Subject: Better way to build a site than or with JSPs ?
>
> I've experimented and have found one way to templated site with JSP
but
> there has to be a better way. First, I'll explain what I do at the
moment.
> Consider a very simplistical sitemap ~
>
> Homepage
> +-Company
> +-Products
> +-Services
> +-Feedback
>
> I've created a folder structure to represent this. And the default
page in
> each folder is index.jsp. Each page (index.jsp) shares the same banner
> (Banner.jsp), footer (Footer.jsp) and navigation menu (Menu.jsp) which
go
> in
> the Global folder. So, this gives me the following ~
>
> index.jsp (Homepage)
> \Company\index.jsp
> \Products\index.jsp
> \Services\index.jsp
> \Feedback\index.jsp
> \Global\Banner.jsp
> \Menu.jsp
> \Footer.jsp
>
> So far, so good. Next, to make it easier to edit the content on each
page
> and to make it easier to have uniform consistency of pages, EVERY
> index.jsp
> is IDENTICAL ~
>
> index.jsp
> ---------
> <%@ include file="/Global/Banner.jsp" %>
> <table>
> <tr>
> <td><%@ include file="/Global/Menu.jsp" %></td>
> <td><%@ include file="body.jsp" %></td>
> </tr>
> </table>
> <%@ include file="/Global/Footer.jsp" %>
>
>
> Using this, the final file structure is ~
>
> index.jsp (Homepage)
> body.jsp
> \Company\index.jsp
> \body.jsp
> \Products\index.jsp
> \body.jsp
> \Services\index.jsp
> \body.jsp
> \Feedback\index.jsp
> \body.jsp
> \Global\Banner.jsp
> \Menu.jsp
> \Footer.jsp
>
>
> And you know, this system works for me. To edit any page, all I have
to
> do
> it edit the body.jsp which means that I have little risk of messing up
the
> template.
>
> The problem with this is obviously all those identical index.jsp's !!
If I
> have to make a change to the layout, then I have to change all of
them.
> That's obviously BAD but is a simple search/replace because they're
all
> identical anyway.
>
> I know I can use Velocity within JSP in order to use one template for
all
> the JSPs but I still end up with literally hundreds of identical
index.jsp
> pages for a real site with many levels of subdirectories. It'll be
easier
> to
> change the template for layout but really whenever I see repetition on
the
> scale of all those index.jsp's I think something's inefficient.
>
> There has to be a better way of doing this, is there ?
>
> Soefara.
>
> _________________________________________________________________
> Chat with friends online, try MSN Messenger: http://messenger.msn.com
>
>
========================================================================
==
> =
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-
> INTEREST".
> For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
> DIGEST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
> http://archives.java.sun.com/jsp-interest.html
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.jsp
> http://www.jguru.com/faq/index.jsp
> http://www.jspinsider.com
==========================================================================To
unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://archives.java.sun.com/jsp-interest.html
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.jsp
http://www.jguru.com/faq/index.jsp
http://www.jspinsider.com