Over a million developers have joined DZone.

Templating for Java Web Applications

DZone 's Guide to

Templating for Java Web Applications

· Java Zone ·
Free Resource

A while ago I asked the following question on stackoverflow: “Why would I use a template engine?”. I compared template engines to simple JSP includes.

Although I didn’t particularly fancy any of the answer, it was obvious that templating is the preferred way of defining the structure of websites, especially if they are bigger ones. Since then I have used velocty, freemarker, tiles, sitemesh and grails. I hate freemarker, because of its cryptic syntax, its illogical behaviour at times (e.g. only strings as map keys) and because of throwing exceptions when something is null (even if nothing is invoked on the null). I don’t particularly like velocity (apart from using it for email templates) – there’s a learning curve and you don’t get more than a JSP. I didn’t like tiles and sitemesh because they require some external xml configuration that makes navigating through the code harder and they are not exactly very active projects. And while all of these worked and I wouldn’t say “don’t use them” (apart from freemarker), they didn’t feel good.

When working with a grails application, I very much liked GSP (groovy server pages) – it is simple but powerful and has decent IDE support. It has all the good parts of JSP combined with simple if/else constructs and with built-in templating and some extra features. Using it in a non-grails application, I thought, would be an issue. It seems that it is possible, and I’d recommend that, although I haven’t used it outside of grails – here’s how to do it.

But there seems to be a way to have templates with plain JSPs as well (since v2). Everything you need to know is explained in this answer. In short – you use custom .tag files to define de-facto templates. Just make a WEB-INF/tags/templates folder and put your templateName.tag there. Then use in a regular JSP. It is really simple and you don’t need any 3rd party library.

These are the two options I recommend – GSP and JSP. They are simple, usable by front-end developers that do not have experience with Java and the code is rather readable. 


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}