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

发布于 2024-07-25 06:17:55 字数 346 浏览 8 评论 0原文

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

I am writing a web app that would require the hunchentoot web server. I have almost no working knowledge of hunchentoot, or any web server for that matter, and I am wondering how my app written in Common Lisp would serve pages to a web client. I have seen some excellent examples (e.g. Hunchentoot Primer, Lisp for the Web) esp. the one listed on the Hunchentoot page. Do you know where I can find more of such examples?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫卆 2024-08-01 06:17:55

我想知道我用 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 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*)))

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

I am wondering how my app written in Common Lisp would serve pages to a web client.

Hunchentoot serves all things that are in its *dispatch-table*, which is just a list of dispatch handlers.

The simplest thing to do is to serve a static file. One typical example would be a CSS file:

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

For a web application, you would most likely want to dynamically create a web page. You do this by defining a function that returns the page as a string (e.g. with CL-WHO), then creating a handler for this function:

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

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

You can eliminate a lot of boilerplate through macros, by the way:

(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*)))

The examples you have found should be sufficient to get you started, and if you run into problems, read the manual, then ask concrete questions.

清风疏影 2024-08-01 06:17:55

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

我认为 Elephant 发行版中有一个使用 Hunchentoot 的示例(Elephant < /a>作为 Common Lisp 的持久对象数据库。)

define-easy-handler registers the handler you are defining automatically in a global variable which gets checked when a HTTP request arrives (the variable is called *easy-handler-alist*). So it's being taken care of automatically. Do you want to use a handler of a different form than the one defined in the tutorial?

I think there is an example using Hunchentoot in the Elephant distribution (Elephant being a Persistent Object Database for Common Lisp.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文