Why would I use a templating engine? jsp include and jstl vs tiles, freemarker, velocity, sitemesh

A few arguments for Velocity (I haven’t used Freemarker):

  • Potential to re-use templates outside of a web context, such as in sending emails
  • Velocity’s template language syntax is far simpler than JSP EL or tag libraries
  • Strict separation of view logic from any other sort of logic – no possible option to drop down to using scriptlet tags and doing nasty things in your templates.

Placeholders – do velocity/freemaker give anything more than JSTL? In JSTL you put placeholder, and use the model (placed in request or session scope, by controllers) to fill these placeholders.

Yes, references are really the core of VTL:

<b>Hello $username!</b>

or

#if($listFromModel.size() > 1)
    You have many entries!
#end

Efficiency – <%@ include file="file.jsp" %> is more efficient than anything else, because it is compiled once. All other options are parsed/executed many times.

Not so sure I agree with or understand this point. Velocity has an option to cache templates, meaning the abstract syntax tree they are parsed into will be cached rather than read from disk each time. Either way (and I don’t have solid numbers for this), Velocity has always just felt fast for me.

Layout reorganization – if you want to move the breadcrumb above the menu, or the login box above another side-panel. If page inclusions (with jsp) is not well organized, you might need to change every single page in such cases. But if your layout is not overly complex, and you put the common things in header/footer, there is nothing to worry about.

The difference is, with a JSP approach, wouldn’t you be re-organzing this layout in every JSP file that uses the same header/footer? Tiles and SiteMesh allow you to specify a base layout page (JSP, Velocity template, etc – both are JSP frameworks at their heart) where you can specify whatever you want and then just delegate to a “content” fragment/template for the main content. This means there would be just one file to move the header in.

Leave a Comment