Compojure / Ring:无法使会话正常工作

时间:2014-04-11 13:36:21

标签: session clojure compojure ring

我显然遗漏了一些明显的东西,但是:

(ns session-demo.core
  (:gen-class)
  (:use compojure.core
        ring.middleware.session
        [hiccup core page form])
  (:require [compojure.route :as route]
            [compojure.handler :as handler]))

(defn index-page []
    (html5
     [:body
      (form-to [:get "/status"]
               (submit-button "Proceed"))]))

(defn status-page [params]
  (html5
   [:body
    [:p "Session: " (:session params)]
    [:p "Params: " params]]))

(defroutes the-routes
  (GET "/" [] (index-page))
  (GET "/status" {session :session :as params} (status-page params)))

(def app
  (-> (handler/site the-routes)
      wrap-session))

点击“继续”按钮后显示:

Session: {}

Params: {:ssl-client-cert nil, :remote-addr "127.0.0.1", :scheme :http, :query-params {}, :session {}, :form-params {}, :multipart-params {}, :request-method :get, :query-string nil, :route-params {}, :content-type nil, :cookies {"org.cups.sid" {:value "72d6fc6a299669e6332da6eb72964f97"}}, :uri "/status", :server-name "localhost", :params {}, :headers {"accept-encoding" "gzip, deflate", "user-agent" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:28.0) Gecko/20100101 Firefox/28.0", "referer" "http://localhost:3000/", "connection" "keep-alive", "accept-language" "en-US,en;q=0.5", "accept" "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "host" "localhost:3000", "cookie" "org.cups.sid=72d6fc6a299669e6332da6eb72964f97"}, :content-length nil, :server-port 3000, :character-encoding nil, :body #, :flash nil}

我希望在(:session params)中设置一些有意义的东西......

1 个答案:

答案 0 :(得分:2)

会话开始为空。要在会话中放置一些内容,您需要从处理程序返回一个地图,并在密钥:session下提供更新的会话数据。

这样的事情可能是:

(defn index-page []
  {:body (html5
          [:body
           (form-to [:get "/status"]
                    (submit-button "Proceed"))])
   :session {:test [1 2 3]}})