Question

I'm desperately looking for a Java HTML view engine that fulfills three main requirements:

  1. Support for master template pages.
  2. HTML templates can be used as subviews within other templates.
  3. HTML templates do not have to be backed by config files or Java classes.

It's for a Java web application that mainly consists of read-only page and a few pages with forms. Most likely I'll use it in combination with an MVC framework.

Master Template Pages

The main structure of the HTML should be defined by a master HTML page. The different pages just provide the core content that's put into the master page to create the final page. This is more than just the inclusion of header and footer.

Subviews

A page should be able to use other HTML pages/templates as subviews within its own content. It should be able to pass at least one parameter to provide the data that the subview needs to display. Furthermore, it should be possible to recursively use further subviews within a subview. Again, this goes beyond a simple include mechanism.

No Backing

HTML templates should consist of a single file that basically is an HTML or XML page where certain parts will be substituted based on the provided data. They shouldn't need any additional (per template) config files. And they shouldn't have the need to implement any Java classes for backing.

I've already had a look at many Java template engine. But neither of them seems to meet these requirements. (In the .NET world, ASP.NET MVC with the Razor view engine would be a perfect fit though.)

Update:

So far, I've looked at the following engines (please let me know if I've overlooked a way to achieve my requirements with one of these engines):

  • JSP: Has just a very basic inclusion mechanism without master templates or proper scoping for subviews etc.
  • Velocity: Has a slightly advanced inclusion mechanism, but no master pages.
  • FreeMarker: Include mechanism, no master pages.
  • Tapestry: Good component based subviews as well as a templating mechanism. However, it doesn't seem to be easily possible to use template engine part without the rest of the framework, which is too page-centric to be combined with an MVC framework.
  • Tiles: Requires two jsp pages per final page. The two layers (tiles and jsp) makes it too complex, in particular for subviews.

Update (2): I've changed some term: view engine instead of template engine, subviews instead of components.

Was it helpful?

Solution

You haven't really looked at template engines. JSP, Velocity and FreeMarker are not template engines (with your meaning of a template engine). They're languages allowing to generate markup dynamically, and get the data to display in the generated HTML from Java objects. Tapestry is a complete web application framework, based on components.

If you're using JSP to generate the HTML pages, you can use a template engine on top of JSP like Tiles or SIteMesh, which will handle the templating, and thus allow to have one JSP per "component" of a full page. JSP should generaly not be used without an web MVC framework like Stripes, Spring MVC or Struts2. All of these either have their own templating support, and/or support integrating another one like SiteMesh or Tiles.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top