We are the Dev Teams of
  • brands
  • ebay_main
  • ebay
  • mobile
<
>
BLOG

Google Closure Templates integration with Spring MVC

by Mateusz Szczap

Have you ever heard of a Google Closure Templates library? It's a logic-less templates library from Google. Having experimented with this in a few minor projects at mobile - we like this libray and have previously used Freemarker templates when our templates code became very complex. We were looking for a simpler solution that would effectively prevent us from leaking business logic to our templates. We had complex logic in our view layer, which over time became a nightmare to maintain. While this logic should have been coded in the backend and frontend so it would just show the view, this kind of discipline was not always maintained. As we use Spring MVC in a number of projects we found that every team came up with its own solution. This blog post is about integrating Google's Closure Templates library and Spring MVC.

Those that like using Google's Soy Library are often faced with hurdles and challenges when plugging it in to a Java Spring MVC application. There simply isn't any standard implementation of Spring MVC's View and TemplateViewResolver classes (org.springframework.web.servlet.View and org.springframework.web.servlet.ViewResolver). There are some libraries in the open source world but none that would have satisfied our project requirements. A number of websites at mobile.de are now  running using the spring-soy-view library to address the aforementioned problem (implementing Spring's View interface using Google Soy Template Library).

Meet Google Soy Template Language
If you are not familiar with Soy, it's a templating library that is used for rendering templates and has been used at mobile.de on a number of projects. The library originates from Google and is used in products such as Google Docs or Gmail.

The library has a few advantages that make it unique in the template library market:

  • Simplicity, the library contains a few simple tags to allow the frontend code to be relatively simple whilst complexity can be added to the backend (statically typed and hopefully with good unit test coverage)
  • Good design, calling any methods from template to a java layer is not permitted
  • Java and JavaScript compiler, it is possible to compile a soy template file to java objects on the server side or a javascript file and render a template by invoking a javascript function
  • Generic Template Data, as the name suggests it is not specific to any programming language, the same template can be used in JavaScript and Java. Data that is passed into templates for processing is valid as long as it's structured as a tree with a number of primitive data types such as, string, boolean, int, map or a list
  • I18N support, through {msg} tag, this is a special plugin that parses soy files and replaces i18n msg tags with translated strings
  • Global Compile time variables, variables that are known at compile time can be passed directly to soy so that they are available on all templates, e.g. homePageUrl, debugOn, etc.
  • Global Runtime variables, variables that are constant during session and are available on all templates, e.g. loggedInCustomerId (they change with lifecycle of application)
  • XSS vulnerabilities security, soy contains built-in support (which is optional) to prevent common XSS vulnerabilities
  • Syntax highlighting plugins, for common editors (IntelliJ, Eclipse, Emacs, Vim, etc)
  • Plugins support, if you are not happy with what Soy Closure provides then you can use or write a soy plugin to extend it. They use Google Guice internally.

Why Spring Soy View Library was created?

One of the problems I noticed at mobile.de was that we were all creating our own small libraries (due to there being no good open source implementation).  In some areas people implemented Google's Soy Closure but there didn't seem to be a solution that met all of our needs. In my free time I decided to create an open source library that would address those problems and allow me to use it for a mobile.de project and at the same time allow the Open Source community to benefit from it. Not only is fragmentation and creating many libraries not good practice due to lack of code reuse but more importantly I noticed that there were semi-implemented features and solutions that had been quickly rushed, which often crippled the design.  The worse thing is that I noticed that people also copied these half-baked solutions to other projects.


Meet Spring Soy View Library

Having observed these problems and what technical debt people introduced I decided to write an Open Source library - Spring Soy View. The shortest possible description I came up with is that it is a librrary that is a bridge between Google Closure Templates and Spring MVC.

The library supports the following features (mostly via pluggable interfaces) with a default implementation:

  • Spring MVC friendly (TemplateViewResolver and View implementation)
  • Compatibility with Spring's (@Configuration annotation) used to wire components and xml
  • Rendering based on models in nested POJO objects or simple data structures (e.g. maps in maps, lists, strings. etc)
  • I18N Support
  • Out-of-box JavaScript compilation support via Spring's controller that compiles to JavaScript. Support for cacheable hashed urls in production and non cache busted urls in dev mode
  • Debug mode support, hot reloading of soy files for developers working in dev mode
  • Caching support to make it fast in a production environment, where hot reloading of Soy files is not needed
  • Soy Global Compile Time Variables support
  • Soy Runtime Variables support
  • Implementation hidden behind well defined interfaces with default implementation
  • Open Source community involvement - number of pull requests
  • The library is open sourced under the commercially-friendly Apache 2 License
  • The project is available on GitHub and available for everyone to use - (https://github.com/matiwinnetou/spring-soy-view)
  • The project library has been pushed to the maven central repository and there has been a number of releases already
  • An example app is available: https://github.com/matiwinnetou/spring-soy-view-example
  • Has been used in production at mobile.de and a few other companies globally

Enjoy!

?>