2014-04-11 35 views
2

我清楚地失去了一些東西很明顯,但:的Compojure /振鈴:無法獲取會話工作

(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} 

我所期待的東西有意設置(:會話參數)...

+0

如果我沒有記錯的':session'保存與屬於該HTTP會話,它默認存儲在cookie值的地圖。所以除非你在':session'映射中加入了一些東西(通過返回一個帶有修改的':session'映射的響應),否則你不會得到任何回報。我發現你有一個名爲'org.cups.sid'的cookie,如果你在'wrap-session'函數中提供了選項映射'{:cookie-name'org.cups.sid「}',它將使用該cookie來存儲您的會話數據。 –

+0

也嘗試過(wrap-session {:cookie-name「org.cups.sid」}),導致相同的輸出... –

+0

如果安全性有問題,應該使用內存中會話,而不是cookie基於一個。這裏的問題是,直到你把它放在那裏,會話纔會有任何東西。 – noisesmith

回答

2

會話開始爲空。要在會話中放置某些東西,您需要從您的處理程序返回地圖,並在關鍵字:session下提供更新後的會話數據。

事情是這樣的,也許:

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