; 一些辅助函数
(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:
<!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,不知道如何将这个path.web.lisp与下面的index.tmpl写在同一个目录下
发布于 2012-04-13 23:54:00
一个显而易见的问题是“你评估过start-web了吗?”答案可能是“是”,但请注意,为了让您的服务器监听适当的端口,您确实需要调用start。如果您看到Hunchentoot错误页面,这不是问题所在。
fill-and-print-template是如何定义的?如果它需要绝对路径名,您可能需要执行(merge-pathnames "index.tmpl"),而不是传递相对路径。
一般来说,有几件事可以让lisp web开发变得更容易。
asdf:load-op就可以使用ql:quickload了),hunchentoot:easy-acceptor和define-easy-handler来定义你的页面(这是一点语法糖,让你定义一个处理程序函数,同时将适当的调度程序推送到*dispatch-table*),它对(setf hunchentoot:*catch-errors-p* nil) (或(setf hunchentoot:*show-lisp-errors-p* t),取决于您的喜好)很有帮助
https://stackoverflow.com/questions/10138067
复制相似问题