web的功能:把数据库(文本文件)的内容显示到网页上。
1. lein new hello 会产生一个hello文件夹
2. 会在刚产生的hello文件夹里,自动添加一个project.clj文件,内容如下: (不一样可以改)
(defproject hello "0.1.0-SNAPSHOT" :description "FIXME: write description" :url "http://example.com/FIXME" :dependencies [[org.clojure/clojure "1.4.0"] [compojure "1.1.1"]] :plugins [[lein-ring "0.7.3"]] :ring {:handler hello.handler/app} :profiles {:dev {:dependencies [[ring-mock "0.1.3"]]}})
3. 把src/hello/handler.clj的内容改成:
(ns hello.handler (:use compojure.core) (:require [compojure.handler :as handler] [compojure.route :as route]) (:import (java.io BufferedReader FileReader))) (defn db-to-html [file] (with-open [rdr (BufferedReader. (FileReader. file))] (apply str (map (fn [line] (str "<h1>" ((read-string line) :title) "</h1><br><p>" ((read-string line) :body) "</p>\n<hr>")) (line-seq rdr))))) (defroutes app-routes (GET "/" [] (db-to-html "f:/hello/res/a.db")) (route/not-found "Not Found")) (def app (handler/site app-routes))
3. 在hello文件夹下添加res/a.db文件(其实就是文本文件,暂时当做数据库用)
a.db文件的内容是
{:title "标题", :body "文摘内容"} 。。。 。。。
4. CD HELLO -> lein ring server启动服务器