Posts Freemarker vs. JSP (JSTL EL)

Freemarker vs. JSP (JSTL EL)

I’m starting to wonder what more can Freemarker offer than JSP 2.0 (or even 1.2) with JSTL EL (Taglibs)?
Still it isn’t clear separation (as claimed by FM folks) between the Ms and Vs.

Moreover (for someone like me), embedding JSP Tag libs (being it Struts, JSTL, or the like) into Freemarker templates is equivalent to using JSPs with JSTL but with the subtracted benifit of adding java snippets to the page.
OK, OK, I can hear screams about the last sentence, but, what’s the harm in <%= (new java.util.Date()).toLocaleString() %> inside a JSP? (I can’t still figure out how to echo the current date in Freemarker)

One more thing – Does the word “Specifications” mean anything?

Would like to hear opinions about Freemarker.

Migrated Comment:
Disclaimer: I’m one of FreeMarker admins, so I’m certainly biased. That said, there are couple of things where FreeMarker might suit you better:

  1. This is subjective, but the overall syntax is less awkward. I’m aware there’s lot of syntacic sugar in JSP 2.0 that’s aimed at hiding the ugliness of directly using JSTL tags for control flow etc. but remember that FreeMarker has had a rather ergonomic syntax for “foreach” and “if” for years.

  2. Macros. If you want to have reusable template snippets, you can write them as macros and import them. AFAIK, in JSP 2.0 you finally have a sort of macros, but you need to define each snippet in its own file. This is nowhere as elegant as the full namespace/macro solution in FreeMarker.

  3. Transforms. Transforms are the equivalent of body tags in JSP, with a significant distinction that while JSP body tags first cache all of their body and only after that act on them, FreeMarker transforms are fully output-driven, in fact they are implemented internally as chains of objects. Therefore, regardless of how many transforms you have nested, you can still have progressive page rendering (“can” because a transform can choose to cache all of its body like a JSP body tag, but is not mandated to do so and can provide a clever implementation that allows progressive processing).

  4. Also, the usual argument about usability outside the HTTP protocol stack. If your system does any text output beside HTTP responses – i.e. sends SMTP mail, you can use FreeMarker for generating mail messages from templates, too, or for generating arbitrary XML. It even has declarative XML processing capabilities which make it possible to use it similar to XSLT, but significantly less complex. You can simplify your project by having one language for all your text output generating needs.

  5. Data model flexibility – if you like, you can implement the data model interfaces to naturally represent any underlying data model. I.e. we have a wrapper for Jython objects that pretty faithfully reflects the look and feel of Jython objects in the template syntax. People have reported providing a similar binding for JavaScript etc.

There’s also lots of goodies like automatic HTML or XML escaping of expressions, convenient literal syntax for lists and maps, etc.

This much off the top of my head – it’s a bit late here, so I can’t think of anything else at this very moment.

If you want to show the current date… add it to the model. I believe that freemarker not allowing you to instantiate the object is a GOOD thing.

@ken – it has been a while since I last played with FreeMarker. I would say that date output is probably more of a helper, rather than model domain. I agree that separation in king, but my example was referring to using the language within a template (which I would say should still be acceptable for display logic).


Search Results