Over a million developers have joined DZone.

Templating for Java Web Applications

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

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. 

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

Published at DZone with permission of Bozhidar Bozhanov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}