Hunchentoot:为什么我不能获得会话值?

时间:2014-03-23 21:33:00

标签: common-lisp hunchentoot

不是CL也不是Web编程专家,所以我可能会遗漏一些非常明显的东西:我尝试在第1页设置会话值并在第2页中获取结果。第2页没有显示任何内容,但是......

(ql:quickload "cl-who")
(ql:quickload "hunchentoot")

(defpackage :sessmin
  (:use :cl :cl-who :hunchentoot))

(in-package :sessmin)

(defun start-server (port)
  (start (make-instance 'easy-acceptor :port port)))

(setf (html-mode) :html5)

(define-easy-handler (page1 :uri "/page1") ()
  (start-session)
  (setf (session-value :sv) "testvalue")
  (with-html-output-to-string
      (*standard-output* nil :prologue t :indent t)
    (:html :lang "en"
       (:head 
        (:meta :charset "utf-8")
        (:title "page1"))
       (:body 
         (:p "Session Page 1")
         (:p "Go to next page" (:a :href "page2" "here"))))))

(define-easy-handler (page2 :uri "/page2") ()
  (with-html-output-to-string
      (*standard-output* nil :prologue t :indent t)
    (:html :lang "en"
       (:head 
        (:meta :charset "utf-8")
        (:title "page2"))
       (:body 
         (:p "Session Page 2")
         (:p "Session Page 2, value:" (session-value :sv))))))

(start-server 8080)

编辑:得到了“(”我的第一个版本错了,但在修正后仍然无效......

2 个答案:

答案 0 :(得分:2)

不是Hunchentoot专家,但我想您忘了关闭:head

答案 1 :(得分:2)

您的问题不是没有设置会话值,而是以您的cl-who格式设置。

(:p "Session Page 2, value:" (session-value :sv))

不会打印会话值; session-value的返回值被忽略。

;; try this:
(:p "Session Page 2, value:" (str (session-value :sv)))
;; or, if you need to html-escape the value,
(:p "Session Page 2, value:" (esc (session-value :sv)))