## Web應用
有很多Clojure類庫可以幫助我們創建web應用。現在比較流行使用Chris Granger寫的 [Noir](http://webnoir.org/) 。另外一個簡單的,基于MVC的框架, 使用Christophe Grand寫的? [Enlive](https://github.com/cgrand/enlive) 來做頁面的template, 是Sean Corfield寫的 [Framework One](https://github.com/seancorfield/fw1-clj) 。另一個流行的選擇是James Reeves寫的Compojure,你可以在這里下載: [http://github.com/weavejester/compojure/tree/master](http://github.com/weavejester/compojure/tree/master) 。所有這些框架都是基于Mark McGranahan寫的 [Ring](https://github.com/mmcgrana/ring) (James Reeves同學現在在維護). 我們以Compojure為例子來稍微介紹一下web應用開發。最新的版本可以通過git來獲取:
```
git clone git://github.com/weavejester/compojure.git
```
這個命令會在當前目錄創建一個叫做 `compojure` 的目錄. 另外你還需要從 [http://cloud.github.com/downloads/weavejester/compojure/deps.zip](http://cloud.github.com/downloads/weavejester/compojure/deps.zip) 下載所有依賴的JAR包,把 `deps.zip` 下載之后解壓在 `compojure` 目錄里面的 `deps` 子目錄里面:
要獲取 `compojure.jar` , 在compojure里面運行 `ant` 命令。
要獲取 Compojure的更新, 切換到 `compojure` 目錄下面執行下面的命令:
```
git pull
ant clean deps jar
```
所有的 `deps` 目錄里面的jar包都必須包含在classpath里面。一個方法是修改我們的 `clj` 腳本,然后用這個腳本來運行web應用. 把 " `-cp $CP` " 添加到 `java` 命令后面去 執行 `clojure.main添加下面這些行到腳本里面去,以把那些jar包包含在` `CP` 里面。
```
# Set CP to a path list that contains clojure.jar
# and possibly some Clojure contrib JAR files.
COMPOJURE_DIR=<em>path-to-compojure-dir</em>
COMPOJURE_JAR=$COMPOJURE_DIR/compojure.jar
CP=$CP:$COMPOJURE_JAR
for file in $COMPOJURE_DIR/deps/*.jar
do
CP=$CP:$file
done
```
下面是他一個簡單的 Compojure web應用:
 
```
(ns com.ociweb.hello
(:use compojure))
(def host "localhost")
(def port 8080)
(def in-path "/hello")
(def out-path "/hello-out")
(defn html-doc
"generates well-formed HTML for a given title and body content"
[title & body]
(html
(doctype :html4)
[:html
[:head [:title title]]
[:body body]]))
; Creates HTML for input form.
(def hello-in
(html-doc "Hello In"
(form-to [:post out-path]
"Name: "
(text-field {:size 10} :name "World")
[:br]
(reset-button "Reset")
(submit-button "Greet"))))
; Creates HTML for result message.
(defn hello-out [name]
(html-doc "Hello Out"
[:h1 "Hello, " name "!"]))
(defroutes hello-service
; The following three lines map HTTP methods
; and URL patterns to response HTML.
(GET in-path hello-in)
(POST out-path (hello-out (params :name)))
(ANY "*" (page-not-found))) ; displays ./public/404.html by default
(println (str "browse http://" host ":" port in-path))
; -> browse http://localhost:8080/hello
(run-server {:port port} "/*" (servl
```