Posts /

Spring Surf meets Clojure

Twitter Facebook Google+
23 May 2010

For those who missed it, some time ago the Alfresco guys donated their Surf Platform to SpringSource, giving birth to the now-called SpringSurf, which is thereby described as:

a view composition framework for Spring MVC that plugs into your existing Spring applications. It provides a scriptable and content-centric approach to building web applications.

I’m not going to introduce you how to use this yet-another MVC framework, but Michael Uzquiano provided an awesome blogpost, in case you were interested into learning more about it.

Part of the job of this Spring Surf framework is to provide an easy to use web scripting framework, REST like, that enables you to easily put together a View and, optionally, a Controller to implement a web API, provide them in the classpath together with a small XML descriptor, and your REST API is deployed right away. In the SpringSurf parlance, this is called called WebScript. Again, technical insights about the framework are better explained elsewhere, so no repetitions here. What I think is cool about Spring Surf, and I’m here with you to discuss, is it’s multi-language focus: web script Controllers can be written in Javascript, PHP or Groovy right out of the box, allowing you to choose whichever language you prefer. Moreover, if you just peek at Spring Surf source code, plugging in support for new languages doesn’t look so hard, so I decided to challenge myself and write support for Clojure backed webscripts. You can find the result of my efforts on github, with a sample webscript that proves the concept.

Following the webscript framework praxis, a model map object is passed around that acts as a container for whichever result your computation will produce, that will at the end handed over to a view rendition engine, Freemarker in our case, to build the resulting document. Webscripts are intended to support a number of different output formats, from JSON to XML to plain text, so being generic here is not an option.

Anyway, the first thing you usually do is to populate the model map with a number of objects that will be needed by the rendering engine to build the response. To support this use case, this first implementation of the Clojure backend for Spring Surf webscripts expects your Clojure “script” to yield a map, in Clojure sense. This map will be then forwarded to a Freemarker template and used to build a webpage or whatever the user asked for, i.e. this controller:

(ns web.script.test)

{:foo "bar"}

and this freemarker template:

foo: ${foo}

will provide “foo: bar” back to the client.

This is just an experiment at the moment, and far from being production-quality stuff. Still, it has been instructing to build a Java-to-Clojure integration, and nonetheless it might come in handy when I’ll be trying to put more Clojure in my working life.


Twitter Facebook Google+