如何将Web应用程序连接到Hunchentoot

4 common-lisp hunchentoot

我正在编写一个需要hunchentoot Web服务器的Web应用程序.我几乎没有关于hunchentoot或任何网络服务器的工作知识,我想知道我在Common Lisp中编写的应用程序如何将页面提供给Web客户端.我已经看到了一些很好的例子(例如Hunchentoot Primer,用于Web的Lisp).Hunchentoot页面上列出的那个.你知道我在哪里可以找到更多这样的例子吗?谢谢.

Sva*_*nte 8

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

Hunchentoot服务于其中的所有内容*dispatch-table*,这只是调度处理程序的列表.

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

(push (create-static-file-dispatcher-and-handler "/example.css"
                                                 "example.css")
      *dispatch-table*)
Run Code Online (Sandbox Code Playgroud)

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

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

(push (create-prefix-dispatcher "/foo.html" 'foo)
      *dispatch-table*)
Run Code Online (Sandbox Code Playgroud)

通过以下方式,您可以通过宏消除大量样板:

(defmacro standard-page ((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"
          (:head 
           (:meta :http-equiv "Content-Type" 
                      :content "text/html;charset=utf-8")
           (:title ,title)
               (:link :type "text/css" 
              :rel "stylesheet"
              :href "/example.css"))
              (:body
               ,@body))))

(defmacro defpage (name (title) &body body)
  `(progn
     (defmethod ,name ()
       (standard-page (,title)
         ,@body))
     (push (create-prefix-dispatcher ,(format nil "/~(~a~).html" name) ',name)
           *dispatch-table*)))

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