I suppose this is a strange question to the huge majority of programmers that work daily with Java. I don't. I know Java-the-language, because I worked on Java projects, but not Java-the-world. I never made a web app from scratch in Java. If I have to do it with Python, Ruby, I know where to go (Django or Rails), but if I want to make a web application in Clojure, not because I'm forced to live in a Java world, but because I like the language and I want to give it a try, what libraries and frameworks should I use?
当前回答
你也可以看看下面这些框架(摘自clojure/projects):
级联 召唤
关于Stack Overflow还有一个相关的问题:成熟的Clojure web框架?
其他回答
你也可以尝试Clojure线圈,http://github.com/zubairq/coils -免责声明:我是作者
到目前为止,我所遇到的最好的Clojure web框架是Compojure: http://github.com/weavejester/compojure/tree/master
它很小,但功能强大,并且具有漂亮优雅的语法。(它在底层使用Jetty,但它对您隐藏了Servlet API,除非您需要它,但这种情况不会经常发生)。去看看那个URL的README,然后下载一个快照并开始播放。
如今,Pedestal是一个值得一看的框架。它是一个构建在Ring之上的服务器端框架,但是通过暂停和恢复特定的请求(否则缓慢的请求实际上会阻塞服务器线程),也可以从初始线程中释放传入的请求。可能有点像JavaBean。
其他很酷的框架还有hoplon。io和David Nolen的Om(基于React)
我将对来自@weavejester (Compojure和Ring的维护者)的pipe发表我的意见。
它的核心是将Component和Ring路由器放在一个屋檐下。我使用管道的原因:
Excellent philosophical foundation: it encourages you to build your app as a series of small components, and it strikes a nice balance between holding few opinions while providing sane defaults. Stable path: I speak for myself, but over the years I've felt that the Clojure community has presented one less-than-credible web framework after another. A couple simply felt too experimental (my experience with Om and client-side Pedestal) for "getting things done" (not that they won't prove superior down the road). On the other hand, I feel like @weavejester has brought the same stability and measured progress to Duct that he did to Compojure and Ring, which have been superbly born out in the community. It's super lightweight, and out of the way of my components.
主要特点:
通过“端点”组织路由,你可以把这些小组件想象成迷你web服务器(或者,HTTP路由的小截面)。 重新加载工作流的开箱即用支持。 与Ring和Compojure完美集成。 开发和生产配置(这是我在其他地方发现的明显缺失的部分)。 带有示例的良好文档。
注意:这是不言而喻的,但对于web开发新手来说,就像大多数Clojurey的东西一样,pipe要求扎实掌握Clojure语言。我还建议先阅读Component。
在另一个个人注意,我已经在几个生产应用程序中使用了一年多了,对它非常满意。
Compojure is no longer a complete framework for developing web applications. Since the 0.4 release, compojure has been broken off into several projects. Ring provides the foundation by abstracting away the HTTP request and response process. Ring will parse the incoming request and generate a map containing all of the parts of the request such as uri, server-name and request-method. The application will then handle the request and based on the request generate a response. A response is represented as a map containing the following keys: status, headers, and body. So a simple application would look like:
(def app [req]
(if (= "/home" (:uri req))
{:status 200
:body "<h3>Welcome Home</h3>"}
{:status 200
:body "<a href='/home'>Go Home!</a>"}))
One other part of Ring is the concept of middle-ware. This is code that sits between the handler and the incoming request and/or the outgoing response. Some built in middle-ware include sessions and stacktrace. The session middle-ware will add a :session key to the request map that contains all of the session info for the user making the request. If the :session key is present in the response map, it will be stored for the next request made by the current user. While the stack trace middle-ware will capture any exceptions that occur while processing the request and generate a stack trace that is sent back as the response if any exceptions do occur.
直接使用Ring可能会很乏味,所以Compojure是建立在Ring之上的,将细节抽象出来。应用程序现在可以用路由来表达,所以你可以有这样的东西:
(defroutes my-routes
(GET "/" [] "<h1>Hello all!</h1>")
(GET "/user/:id" [id] (str "<h1>Hello " id "</h1>")))
Compojure仍然在使用请求/响应映射,所以你可以在需要的时候访问它们:
(defroutes my-routes
(GET "*" {uri :uri}
{:staus 200 :body (str "The uri of the current page is: " uri)}))
在这种情况下,{uri:uri}部分访问请求映射中的:uri键,并将uri设置为该值。
最后一个组件是Hiccup,它使生成html更容易。各种html标记被表示为向量,第一个元素表示标记名,其余元素是标记体。"<h2>A header</h2>"变为[:h2 "A header "]。标记的属性位于可选映射中。"<a href='/login'>登录页面</a>"变为[:a {:href "/login"} "登录页面"]。下面是一个使用模板生成html的小示例。
(defn layout [title & body]
(html
[:head [:title title]]
[:body [:h1.header title] body]))
(defn say-hello [name]
(layout "Welcome Page" [:h3 (str "Hello " name)]))
(defn hiccup-routes
(GET "/user/:name" [name] (say-hello name)))
这里是compojure作者目前正在编写的一些文档的草稿的链接,您可能会发现这些文档很有用:compojure Doc