Where are all the JSP's? That is a question we have been hearing a lot lately, after deciding that the view layer of Clearspace would be built on an open alternative to JSP, Freemarker. What is Freemarker? Freemarker is simply a "template engine"; a generic tool to generate text output based on tempaltes. The next question that usually follows: Why?
Common problems with JSP
Java code is too tempting, even though it is considered bad design to mix presentation with core logic, everyone knowingly does it.
Java code is required to do mundane things, such as <a href="<%= request.getContextPath() %>/index.html">Home page</a>
Simple tasks are difficult, such as escaping html, or formatting a date
Looping is verbose, awkward and messy
Bad exception handling, printing out things like NullPointerExceptions to the user
Need a compiler and any change needs to be recompiled.
Various application server bugs. We have recently run into a few problems on 'unnamed' application servers where the jsp's are created importing the servlet package without fully qualifying names, This causes us to have class name problems.
Freemarker as an alternative
To fully realize the benefits of the MVC Pattern, a true separation of Java Code (the controller) and the HTML (the view) is required. Using a template engine, such as Freemarker, as your view will solve many of the problems listed above. The underlying design will be cleaner, the syntax clearer, the error messages more meaningful, and the application more customizable.
As an example of how Freemarker can cleanup your view pages, lets take one line of JSP and convert it to freemarker:
<% Action action = ActionUtils.getAction(); %>
<input type="text" name="subject"
value="<%= action.getSubject() != null ? StringUtils.escapeHTMLTags(action.getSubject()) : "" %> "/>
<input type="text" name="subject"
Interesting Clearspace features relating to Freemarker
Themes: To help facilitate our customer's need to customize the look and feel of our products, we wrote a feature that provides our customer's the ability to customize most front end pages directly from the admin console without having to make a single change to the source code or even restart the application server. Simply select the page you want to customize and edit the template. Additionally, these custom pages can be grouped into different "Themes" and mapped to a commuity, a forum, even a URL pattern. This type of flexible UI would not be possible with JSP's. Freemarker provides us the ability to load the source of a template from a number of places, including a remote server, and without ever needing to be recompiled.
Freemarker Email Templates: Allows for easily customizable dynamic emails with very detailed and useful information.
After using Freemarker for some time, we can definitely say that it is a template language we?re confident in. We found it to be the easiest to use, learn and the most powerful while still having a clean, simple syntax. For a comparision with velocity, check out Freemarker vs. Velocity. We?ve also found that any good JSP programmer finds Freemarker enjoyable to use and learn. The one downside is that we all use Intellij Idea for development and there is currently no Freemarker support.
For those who still are not convinced, rest assured that it is still possible to make customizations to the application using only JSP's.
To learn more, check out FreeMarker: An open alternative to JSP