Before starting a project (standard e-commerce stuff and multi-tier
architecture), the only piece of the architecture I
wasnt sure on was the presentation layer. Servlets are 50% of that with the
other half being a template framework.

Two strategies emerged  as top contenders: JSP and its various flavors and
"pure" templates (Webmacro, dexios.template aka Freemarker). Other
possibilities were generating HTML "by hand" from the servlet using either
"straight" (dumb) out.println("<...>") approach, generating HTML using a
library (WebLogic's htmlKona, ECS), or using a mix of these two. Jason
Hunter does a fairly good job of introducing the readers to the latter two
technologies in his book but omits the templates, and that's a shame, IMO,
because templates are a correct way to go for anything but the most simple
application. (They're not even mentioned in passing in the otherwise
excellent book. *shrug*)

JSP I rejected right away as something very wrong. Not only the syntax was
unappealing (to say the least) but the idea of mixing Java and HTML in the
same page seemed like someone's Javascript-induced nightmare. To elaborate,
my goal was to have an HTML designer and an application developer work more
or less in parallel. The pages and the code would be created at the same
time and integrated daily. This is possible with JSP but the drawbacks are
many, tight coupling between Java and HTML and lack of context (is this is a
servlet? a client-side program? a page? SSI-enabled page ? all of the
above?) being only the tip of the iceberg. JSP does not define exactly how
the directives are to be processed by the JSP processor, which to me spelled
"problem". If most of the CPU and I/O time is spent on evaluating the
template and I dont know what's going on under the hood, it's a high time
for me to look at another option. (As an aside, GNUJsp from bitmechanic.com
looked better than others).

"Pure" templates were really my only choice and there werent many *good*
template frameworks out there. For my project *good* meant stable and
compact with decent documentation. (It's still a shock to me that many
people dont consider documentation an integral part of the project.
Documentation is your *face* but I digress.) I consulted a list of template
packages in Alex Chaffee's FAQ and found only two that satisfied the
requirements: dexios.template (now Freemarker) and WebMacro. I wrote a
sample servlet with a bunch of data being pulled out from the database and
tried using both packages.

My main criteria remained the same: stable and compact. I felt that
Freemarker satisfied them to a greater extent than WebMacro. Several
reasons:

1) Freemarker is template-only package whereas WebMacro tries to go beyond
that with the resource broker(s), template providers and other possibly nice
but unnecessary (in my case) stuff. I wanted nothing more than a way to say
    Template t = new Template(new File("foo.template"));
    t.output(req.getOutputStream());

To do this, I had to write my own provider for WebMacro that would pick up a
file based on the filename rather than search for it in the classpath
(default). Freemarker didnt need this as it's a simpler system based on
files and the above is what it was explicitly designed for.

2) Freemarker has a simple script syntax whereas WebMacro is more like Perl.
Hidden in this seemingly innocuous statement is the implementation detail
that greatly affects the performance of the package: WebMacro uses
introspection extensively to find the data in the supplied context whereas
Freemarker opts for an adapter approach where the template writer has to
write an explicit query for a piece of data based on the supplied context
key. That is, if you have
the following line in your template:

<input type="text" name="name" value="$foo.bar.baz">

WebMacro will look for bar and baz (and possibly foo) using Reflection
whereas Freemarker will force you to write something like

if (key.equals("foo")) return "foobar"

somewhere in your adapter. This is a bit more work but Freemarker's adapter
design is elegant and not much of a hindrance. (If you ever dealt with
java.lang.reflect, you know how "fast" it can be, not to mention potential
stack explosions).

3) Documentation. I found Freemarker's docs to be well-organized, coherent
and with a working walkthrough of a guestbook example. WebMacro has the same
but it's about 50% of what it has to be, partially due to its complexity. I
had problems with WebMacro's
guestbook example due to the default template provider's functionality of
looking for a template in the classpath.

4) Licensing. WebMacro is either GPL or $$$. Freemarker is LGPL (always
free; if you modify its code it becomes very much like GPL). The big
difference between GPL and LGPL is that the former requires you to supply
the source code of your application whereas the latter requires you only to
supply the binaries, standard procedure in any commercial development,
regardless of the company size. LGPL was created precisely for the purpose
of using a library, free of charge even if you will sell the product that
uses it. (LGPL refers to this as "work that uses a library").

and last but not least, the claim that many template packages use as  bait:
"let the designer write HTML and use our simple syntax to control the data
flow". This is a myth as most HTML designers have a difficult time
understanding foreach loops and other programmatic constructs. Usually good
designers come from art/publishing/media side of the pond and are excellent
visual artists with poor grasp of data structure constructs...and that's
good. I'd rather have a strong artist who knows his craft than a
middle-of-the-road designer who knows Javascript. ("Instead of table, you
have a foreach loop with a single row. - <Blank stare>"). Therefore, the
application developer has to take on the responsibility of marking up the
template. As such, lesser syntax complexity and greater programmatic control
is always appreciated.

Comments, flames, etc are welcome. I am particularly interested in knowing
whether there are other template packages unknown to me that can measure up
to Freemarker or WebMacro, both of commercial and/or free (Open Source, GNU)
nature. (In fact, if someone wrote a good commercial package, it'd sell
quite nicely).

Alex.







_______________________________________________________________
Get Free Email and Do More On The Web. Visit http://www.msn.com

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to