Mature Clojure web frameworks? [closed]

拥有回忆 提交于 2019-12-17 21:24:50

问题


What are the current choices of mature Clojure web frameworks? I am looking for some kind of feature matrix telling me what the popular frameworks support and to what extent, including:

  • Response templating (response written in Clojure or in some other markup - e.g. like JSP with Tiles)
  • HTTP sessions
  • REST with automatic mapping of URLs into action-functions and params
  • HTML forms (params available as map, error handling, validation)
  • Application flow (known from Java frameworks - request handlers return action identifiers which are eventually handled by renderers)

回答1:


Perhaps my answer to the What is the good starting point to developing RESTful web service in Clojure? question on SO might be of help to you. It mentions some important web libraries for Clojure (with links and short summaries). The key point which I would like to reiterate here is stated in the first paragraph of that answer:

First of all, I think that you are unlikely to find a single shrinkwrapped solution to do all this in Clojure (except in the form of a Java library to be used through interop). What is becoming Clojure's standard Web stack comprises a number of libraries which people mix and match in all sorts of ways (since they happily tend to be perfectly compatible).

To that I would add that you should probably not expect to handle things with the sort of "application flow" you might know from Java (or if you believe you really need it, you'll probably have to roll your own lib to support it!). That's alright, though, as people seem to be very happy with Ring's handler-is-a-function, higher-order-middleware-friendly approach.


To address your bullets:

  • Response templating:
    There is a number of Clojure-specific solutions, including Enlive and Hiccup (Enlive is a very powerful HTML scraping / templating / transforming engine; Hiccup is a DSL for writing HTML in Clojure with the nice property that it renders fast). Also, this is probably one place where it makes perfect sense to drop down to Java and use something like, say, StringTemplate. This even has the good side of discouraging the mixing of templates and logic! (I believe Stuart Halloway has mentioned that Relevance -- his company -- is using this strategy in their work and having great success with it.)

  • HTTP sessions
    That would be Sandbar, I suppose. The author has started a series of blogposts about it which looks very promising.

  • REST with automatic mapping of URLs into action-functions and params
    That's Ring & Compojure and/or Moustache. See below.

  • HTML forms (params available as map, error handling, validation)
    As above.

  • Application flow (known from Java frameworks - request handlers return action identifiers which are eventually handled by renderers)
    As mentioned above, not really something people tend to do in Clojure.


As a starting point in learning about the Clojure web stack, this Ring tutorial by Ring's author Mark McGranaghan is very helpful. Compojure's author James Reeves has some documentation on Compojure. Perhaps my recent answer to the What’s the “big idea” behind compojure routes? question might be of help too. Ring's sources also include a great SPEC document.




回答2:


Since this question was originally asked / answered, the Noir web framework has emerged as a promising solution.

It uses hiccup for the templating part, but offers a more complete framework around that.

Basic code sample from the Noir main page:

(ns my-app
  (:use noir.core)
  (:require [noir.server :as server]))

(defpage "/welcome" []
    "Welcome to Noir!")

(server/start 8080)



回答3:


I will recommend you to use Luminus, not because of its awesome name, but also its feature.

And since Noir is no longer maintained, I won't recommend you to use that. It is also a good choice to start from ring & Compojure from the very beginning to build your own framework.




回答4:


You may be asking the wrong question. What I'm seeing in your question is "what Clojure framework is most like the Java Object-Oriented frameworks I'm used to?". There is no good answer to that; if you only feel comfortable with a state-ful server-side approach (such as Grails or Tapestry) then perhaps you should stay there, and find a way to implement some of your backend in Clojure.

On the other hand, if you want to build something more true to Clojure, you may want to find your own mix. I've had some good success using AngularJS and CoffeeScript on the client, and Clojure (using Ring and Bishop) on the server (though we are moving from Bishop to Liberator). In any case, once you embrace the "single page" web app approach and start treating the server-side as a source and sink of data, you'll find Clojure works exceptionally well.




回答5:


try road framework for fast web dev https://github.com/zhujinxian/road

(defn render-test [ret tmt]
  (-> (resp/response "------render----test------") 
    (#(resp/content-type %1 "text/plain"))))

(defn foo
  "I don't do a whole lot."
  [x]
  (str "来自源码目录的参数:" x))

(defn handler [^Integer x]
    {:$r render-test :text (str "hello world, road goes sucess!" (foo x))})

(defn home [req content ^Integer num]
    {:hiccup "home.clj" :content (str "home" content) :num num})

(defroad road (GET "/web-test-0.1.0-SNAPSHOT-standalone/main" handler) 
              (GET "/web-test-0.1.0-SNAPSHOT-standalone/home/:num{\\d+}" home))

(defn -main [& args]
  (log/info "---------log4j test-------")
  (jetty/run-jetty road {:port 3000}))


来源:https://stackoverflow.com/questions/3551805/mature-clojure-web-frameworks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!