10

我正在尝试各种入门示例,我可以得到一个基本的 hello world 示例,在路由中使用基本 HTML

(ns hello-world
  (:use compojure.core ring.adapter.jetty)
  (:require [compojure.route :as route]))

(defroutes example
  (GET "/" [] "<h1>Hello World Wide Web!</h1>"))

(run-jetty example {:port 8080})

但是当我尝试像这样使用 html 助手时

(ns hello-world
  (:use compojure ring.adapter.jetty)
  (:require [compojure.route :as route]))

(defroutes example
  (GET "/" []
    (html [:h1 "Hello World"])))

(run-jetty example {:port 8080})

然后我收到以下错误

[null] 线程“主”java.io.FileNotFoundException 中的异常:无法在类路径上找到 compojure__init.class 或 compojure.clj:(core.clj:1)

4

2 回答 2

9

正如 W55tKQbuRu28Q4xv 在评论中提到的那样,您(:use compojure ...)在第二个示例中使用。您应该切换到(:use compojure.core ...)然后可能为您使用的其他功能引入一些额外的依赖项(例如hiccup(<- 这是 GitHub 存储库的链接),它现在是一个单独的项目,用于构建 HTML 的 DSL)。

我的猜测是,您正在尝试在使用 Compojure 0.4 时遵循为 Compojure 0.3 编写的一些教程。后者根本不包括compojure命名空间,并且已经被精简了很多,基本的 HTTP 处理委托给ring了不同的项目(如前面提到的hiccup)。

幸运的是,有很多关于 0.3 -> 0.4 过渡的资源,例如Brenton Ashworth 的这篇博客文章。如果您无法找到已从 Compojure 中删除的内容,您很有可能现在可以从中了解在哪里找到它。有关勘误表和其他详细信息,另请参阅Compojure 的 Google 组的后续讨论

于 2010-05-26T06:22:36.933 回答
1

我玩过 Compojure “Hello World” 并且遇到了这个问题(以及许多其他在我的大脑中变得混乱的问题)。另一个复杂因素是网络上的许多 Compojure 文档已经过时。最重要的是,这些是您要遵循的步骤:

  1. 拥有最新版本的Leiningen。确保遵循 github 站点上的安装说明。(不要通过macports;他们的 Leiningen 已经过时了。)

  2. 按照此处的Compojure 说明进行操作。

请注意,文件名不正确。它应该是 src/hello_www/core.clj 而不是 src/hello-www/core.clj。

于 2010-05-26T18:00:04.780 回答