Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm about to choose to way to organize my view (with spring-mvc, but that shouldn't matter much)

There are 6 options as far as I see (though they are not mutually exclusive):

  • Tiles
  • Sitemesh
  • Freemarker
  • Velocity
  • <jsp:include>
  • <%@ include file="..">

Tiles and Sitemesh can be grouped; so can Freemarker and Velocity. Which one within each group to use is not a matter of this discussion, there are enough questions and discussions about it.

This is an interesting read, but can't quite convince me to use tiles.

My question is - what do these frameworks give that can't be properly done with <@ include file=".."> and JSTL. Main points (some taken from the article):

  1. Including parts of pages, like header and footer - there isn't a difference between:

    <%@ include file="header.jsp" %> 

    and

    <tiles:insert page="header.jsp" /> 
  2. Defining parameters in the header - like title, meta tags, etc. This is very important, especially from SEO point of view. With the templating options you can simply define a placeholder which each page should define. But so you can in jsp with JSTL, using <c:set> (in the including page) and <c:out> (in the included page)

  3. 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.

  4. Coupling between the common components and the specific content - I don't find an issue with this. If you want to reuse some fragment, move it to a page that doesn't include any header/footer, and include it wherever needed.

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

  6. Complexity - all non-jsp solutions require additional xml files, additional includes, pre-processor configurations, etc. This is both a learning curve and introducing more potential points of failure. Also, it makes support and changing more tedious - you have to check a number of files/configurations in order to understand what's happening.

  7. Placeholders - do velocity/freemarker 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.

So, convince me that I should use any of the above frameworks instead of/in addition to plain JSP.

like image 603
Bozho Avatar asked Jul 02 '10 19:07

Bozho


People also ask

What is an advantage to using a templating engine?

A template engine enables you to use static template files in your application. At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client. This approach makes it easier to design an HTML page.

Is Jstl a template engine?

In addition to JSP/JSTL, there have been a number of template engines released for the Java software ecosystem. These include Apache Velocity, FreeMarker,Thymeleaf and Pippo (which seems to have evolved from an earlier template engine named Pebble). Groovy Server Pages (GSP) are used for Grails/Groovy applications.

Is JSP a template engine?

Spring MVC, Struts or Apache Wicket are examples of web frameworks, whereas JSP, Velocity or FreeMarker are examples of template engines.

Which is better Thymeleaf or FreeMarker?

In the pursuit of comparison between FreeMarker , Thymeleaf, Groovy and Mustache, FreeMarker has the upper hand in performance. However, Thymeleaf wins the battle overall.


1 Answers

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.

like image 64
matt b Avatar answered Oct 08 '22 23:10

matt b