5

我正在编写一个需要 hunchentoot 网络服务器的网络应用程序。我对 hunchentoot 或任何 Web 服务器几乎一无所知,我想知道我用 Common Lisp 编写的应用程序如何为 Web 客户端提供页面。我见过一些优秀的例子(例如Hunchentoot Primer、 Lisp for the Web),尤其是。Hunchentoot 页面上列出的那个。你知道我在哪里可以找到更多这样的例子吗?谢谢。

4

2 回答 2

8

我想知道我用 Common Lisp 编写的应用程序如何将页面提供给 Web 客户端。

Hunchentoot 服务于它的所有东西,*dispatch-table*它只是一个调度处理程序的列表。

最简单的做法是提供静态文件。一个典型的例子是 CSS 文件:

(push (create-static-file-dispatcher-and-handler "/example.css"
                                                 "example.css")
      *dispatch-table*)

对于 Web 应用程序,您很可能希望动态创建网页。为此,您可以定义一个将页面作为字符串返回的函数(例如,使用 CL-WHO),然后为该函数创建一个处理程序:

(defun foo ()
  (with-html-output-to-string ; ...
  ))

(push (create-prefix-dispatcher "/foo.html" 'foo)
      *dispatch-table*)

顺便说一句,您可以通过宏消除很多样板:

(defmacro 标准页 ((title) &body body)
  `(with-html-output-to-string (*standard-output* nil :prologue t :indent t)
     (:html :xmlns "http://www.w3.org/1999/xhtml"
          :xml\:lang "de"
          :lang "de"
          (:头
           (:meta :http-equiv "内容类型"
                      :content "text/html;charset=utf-8")
           (:标题,标题)
               (:链接:输入“文本/css”
              :rel "样式表"
              :href "/example.css"))
              (:身体
               ,@身体))))

(defmacro defpage (name (title) &body body)
  `(预测
     (定义方法,名称()
       (标准页(,标题)
         ,@身体))
     (push (create-prefix-dispatcher ,(format nil "/~(~a~).html" name) ',name)
           *调度表*)))

您找到的示例应该足以让您入门,如果遇到问题,请阅读手册,然后提出具体问题。

于 2009-06-20T23:08:34.480 回答
2

define-easy-handler在全局变量中自动注册您定义的处理程序,该变量在 HTTP 请求到达时进行检查(该变量称为*easy-handler-alist*)。所以它会被自动处理。您想使用与教程中定义的不同形式的处理程序吗?

我认为在 Elephant 发行版中有一个使用 Hunchentoot 的示例(Elephant is a Persistent Object Database for Common Lisp。)

于 2009-07-13T21:13:57.020 回答