2012-04-13 84 views
2
; 一些輔助函數 
(require :asdf) 
(defun loadlib (mod) 
    (asdf:oos 'asdf:load-op mod)) 

(defun reload() 
    (load "web.lisp")) 
(defun restart-web() 
    (progn 
    (reload) 
    (start-web))) 

; load 需要的庫 
(loadlib :html-template) 
(loadlib :hunchentoot) 

; 設置 hunchentoot 編碼 
(defvar *utf-8* (flex:make-external-format :utf-8 :eol-style :lf)) 
(setq hunchentoot:*hunchentoot-default-external-format* *utf-8*) 
; 設置url handler 轉發表 
(push (hunchentoot:create-prefix-dispatcher "/hello" 'hello) hunchentoot:*dispatch-table*) 

; 頁面控制器函數 
(defun hello() 
    (setf (hunchentoot:content-type*) "text/html; charset=utf-8") 
    (with-output-to-string (stream) 
    (html-template:fill-and-print-template 
    #p"index.tmpl" 
    (list :name "Lisp程序員") 
    :stream stream))) 
; 啓動服務器 
(defun start-web (&optional (port 4444)) 
    (hunchentoot:start (make-instance 'hunchentoot:acceptor :port port))) 

模板index.tmpl:爲什麼是Common Lisp的Web程序的執行我不能

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Test Lisp Web</title> 
    </head> 
    <body> 
    <h1>Lisp web開發實例</h1> 
    hi, <!-- TMPL_VAR name --> 
    </body> 
</html> 

當我參觀http://localhost:4444/hello 總是報500錯誤,我懷疑是模板路徑, 我的操作系統是windows, 不知道如何在同一個目錄下用index.tmpl寫這個path.web.lisp

回答

0

顯而易見的問題是「你評價start-web」嗎?這可能是「是」,但請注意,您確實需要撥打start以使您的服務器偵聽適當的端口。如果你得到了Hunchentoot錯誤頁面,這不是問題。

fill-and-print-template如何定義?如果需要絕對路徑名,則可能需要執行(merge-pathnames "index.tmpl")而不是傳遞相對路徑。

爲了讓lisp網站開發更容易,您可以在一般情況下做幾件事。

  • 考慮defining yourself a package。這將讓您有選擇地導入符號,而不是將每個外部符號與它的源代碼包加在一起。它也可以讓你更容易地加載你自己的項目。

  • 考慮使用quicklisp而不是定義自己的load-lib。它可以讓你輕鬆地安裝和加載外部librares(據我所知,如果你指定庫已經安裝,ql:quickload落空至asdf:load-op在任何情況下)

  • 看看cl-who,我覺得這比友好了很多HTML模板化硬盤的方式,因爲你正在做

  • 考慮使用hunchentoot:easy-acceptordefine-easy-handler來定義你的頁面(這是語法糖一點點讓你定義一個處理函數,同時按相應的調度員到*dispatch-table*

  • 在調試Hunchentoot應用時,爲了獲得更好的調試信息,(setf hunchentoot:*catch-errors-p* nil)(或(setf hunchentoot:*show-lisp-errors-p* t),取決於您的偏好)很有幫助。

+0

謝謝!我的問題真的在於路徑,修改路徑。 – user1076871 2012-04-14 17:54:23

相關問題