Compojure/Getting Started

Build from SourceEdit

To download and build Compojure, you will need Git (git), Leiningen (lein), and a Java runtime environment (java).

download latest release from git (ex
cd compojure
lein deps
lein jar

This will generate the compojure.jar file. Put this in your Java classpath.

The deps lein target will download the needed dependencies. If this fails, you can manually download them from:

Unzip the file in the compojure directory and rerun lein.

Hello World with Embedded JettyEdit

Create a shell script called "compojure" to run Clojure with compojure.jar and the deps directory in the classpath:

for f in deps/*.jar; do
java -cp $CLASSPATH clojure.lang.Script $1

or, for Windows:

@echo off
SETLOCAL enabledelayedexpansion

FORĀ %%F IN (deps/*.jar) DO (
  SET cp=!cp!;deps/%%F%

java -cp "compojure.jar;%cp%" clojure.lang.Repl %1%

Then create a Clojure source file called "hello.clj":

(ns hello-world
  (:use compojure))

(defroutes greeter
  (GET "/"
    (html [:h1 "Hello World"])))

(run-server {:port 8080}
  "/*" (servlet greeter))

Next, run this file using the compojure shell script:

./compojure hello.clj

Your web application should now be accessible at: http://localhost:8080/

Hello World in a War fileEdit

First, create your servlet file:

;; src/myapp/MyServlet.clj
(ns myapp.MyServlet
  (:use compojure)
    :extends javax.servlet.http.HttpServlet))

(defroutes greeter
  (GET "/"
    (html [:h1 "Hello world"])))

(defservice greeter)

Next, create your WEB-INF directory:

mkdir -p WEB-INF/lib WEB-INF/classes

Copy compojure.jar, clojure-contrib.jar and clojure.jar to WEB-INF/lib.

You now need to create WEB-INF/web.xml:


Finally, compile the War file. Here is an ant build script that does just that:

 <project name="myapp" basedir="." default="war">
   <property name="build.dir" value="WEB-INF/classes" />
   <property name="source.dir" value="src" />
   <property name="lib.dir" value="WEB-INF/lib" />
   <property name="servlet-api.jar" value="/usr/share/java/servlet-2.3.jar" />
   <property name="warfile" value="myapp.war" />

   <target name="compile">
     <java classname="clojure.lang.Compile">
         <path location="${build.dir}"/>
         <path location="${source.dir}"/>
         <fileset dir="${lib.dir}">
           <include name="*.jar"/>
         <path location="${servlet-api.jar}"/>
       <sysproperty key="clojure.compile.path" value="${build.dir}"/>
       <arg value="myapp.MyServlet"/>

   <target name="war" depends="compile">
     <war warfile="${warfile}" webxml="WEB-INF/web.xml">
       <classes dir="${build.dir}"/>

A bit more verbose exampleEdit

Here is an example of a simple calculator.

(ns example 
  (:use compojure)) 

(defn html-doc 
  [title & body] 
    (doctype :html4) 
        [:title title]] 
	 ;; Pass a map as the first argument to be set as attributes of the element
	 [:a {:href "/"} "Home"]]]

(def sum-form 
  (html-doc "Sum" 
    (form-to [:post "/"] 
      (text-field {:size 3} :x) 
      (text-field {:size 3} :y) 
      (submit-button "=")))) 

(defn result 
  [x y] 
  (let [x (Integer/parseInt x) 
        y (Integer/parseInt y)] 
    (html-doc "Result" 
      x " + " y " = " (+ x y)))) 

(defroutes webservice
  (GET "/" 
  (POST "/" 
    (result (params :x) (params :y)))) 

(run-server {:port 8080} 
  "/*" (servlet webservice))