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?
当前回答
Webjure, Clojure的web编程框架。
特性:分派servlet调用Clojure函数。动态HTML生成。SQL查询接口(通过JDBC)。
这个答案是Webjure信息的占位符。
其他回答
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
我已经成功地在生产环境中使用Liberator一段时间了。如果你只是想要一个基本的框架,例如,如果你正在构建一个RESTful web服务或类似的东西,它是一个很好的框架。它本质上是ring和compojure的包装器,并在验证传入请求时提供决策图。与其他更笨重的web框架相比,它的速度也非常快。如果你想快速开始,然后慢慢建立,那么解放者是一个很好的选择。
你也可以尝试Clojure线圈,http://github.com/zubairq/coils -免责声明:我是作者
Reframe和om。接下来可能就是你要找的了。
Compojure是我用来创建一个小型博客应用程序的工具。它以Sinatra为蓝本,Sinatra是一个最小的、轻量级的Ruby web框架。我主要使用的是路由,就像Sinatra的一样。它看起来是这样的:
(GET "/post/:id/:slug"
(some-function-that-returns-html :id :slug))
没有ORM或模板库,但它有将向量转换为HTML的函数。