There are some good examples on how to compose a web application with Clojure and Vaadin < 7.
However, the web application bootstrapping process has changed for Vaadin 7.
In this post I will show you how to create a Clojure/Vaadin 7 web application.
First we need a web.xml file in src/main/webapp/WEB-INF that defines the Vaadin servlet and parametrizes
it with our main UI class.
:gen-class will make the namespace available as a Java class.
We override the init method of com.vaadin.ui.UI.
This is the entry point for our web application. init in com.vaadin.ui.UI
has only one argument (request). However, our Clojure -init function has two arguments.
The reason is that the function will receive ”this” as the first argument.
Now we can build our UI.
In Java, button actions are triggered by registering a click handler for the button. We have to do this in
Clojure as well but we will hide the actual ClickListener and use a more functional approach.
We create a new function add-action that expects a button and an action as arguments. action can be any Clojure function
we want to be executed when the button is clicked.
The helper function create-button-click-listener wraps a Vaadin ClickListener around the action function.
The project file for building with Leiningen (and packaging with Maven) looks like this:
1234567891011
(defproject cljvaadin7"0.1.0-SNAPSHOT":description"Demonstrates how to integrate Clojure and Vaadin 7":url"https://github.com/codebrickie/cljvaadin7":license{:name"Eclipse Public License":url"http://www.eclipse.org/legal/epl-v10.html"}:dependencies[[org.clojure/clojure"1.4.0"][com.vaadin/vaadin-server"7.0.0"][com.vaadin/vaadin-client-compiled"7.0.0"][com.vaadin/vaadin-themes"7.0.0"][javax.servlet/servlet-api"2.5"]]:aot[cljvaadin7.MyApplicationUI])