原帖地址:http://java.ociweb.com/mark/clojure/article.html#WebApps
作者:R. Mark Volkmann
译者:RoySong
Web应用
有多种Clojure库来创建Web应用。一个常见的选择是采用Compojure,可以从
http://github.com/weavejester/compojure/tree/master
获得。最新的版本可以从Git
资源库中获取到(假设
已经安装了Git):
git clone git://github.com/weavejester/compojure.gitgit clone git://github.com/weavejester/compojure.git
上述命令会在当前目录下建立一个compojure目录,然后从
http://cloud.github.com/downloads/weavejester/compojure/deps.zip
下载所需的jar包,然后将deps.zip放置在
compojure目录下并将它的内容解压到
deps子目录中去。
要构建compojure.jar,在
compojure目录中运行
ant即可。
需要升级Compojure时,在compojure目录中运行以下命令:
git pull
ant clean deps jar
deps子目录下面的所有jar包都必须包含在classpath中,打成这个目的的一种方法是修改
clj脚本,然后用它来
运行web应用。在用来运行clojure.main的
java命令后加上"-cp $CP
",并且在设置CP之前加上以下的命令:
# Set CP to a path list that contains clojure.jar
# and possibly clojure-contrib.jar.
COMPOJURE_DIR=path-to-compojure-dir
COMPOJURE_JAR=$COMPOJURE_DIR/compojure.jar
CP=$CP:$COMPOJURE_JAR
for file in $COMPOJURE_DIR/deps/*.jar
do
CP=$CP:$file
done
下面是一个简单的Compojure Web应用例子:
(ns com.ociweb.hello
(:use compojure))
(def host "localhost")
(def port 8080)
(def in-path "/hello")
(def out-path "/hello-out")
(defn html-doc
"generates well-formed HTML for a given title and body content"
[title & body]
(html
(doctype :html4)
[:html
[:head [:title title]]
[:body body]]))
; Creates HTML for input form.
(def hello-in
(html-doc "Hello In"
(form-to [:post out-path]
"Name: "
(text-field {:size 10} :name "World")
[:br]
(reset-button "Reset")
(submit-button "Greet"))))
; Creates HTML for result message.
(defn hello-out [name]
(html-doc "Hello Out"
[:h1 "Hello, " name "!"]))
(defroutes hello-service
; The following three lines map HTTP methods
; and URL patterns to response HTML.
(GET in-path hello-in)
(POST out-path (hello-out (params :name)))
(ANY "*" (page-not-found))) ; displays ./public/404.html by default
(println (str "browse http://" host ":" port in-path))
; -> browse http://localhost:8080/hello
(run-server {:port port} "/*" (servlet hello-service))
分享到:
相关推荐
clojure-utils, 各种小型但方便的clojure实用程序函数库 各种小型但方便的Clojure实用程序函数库特别关注:Clojure.java - 用于从Java调用Clojure的实用工具函数的Java类arrays.clj - 操作Java数组core.clj - 应该在...
clojure-1.5.1.jar
Clojure入门介绍: Clojure - Functional Programming for the JVM
该版本为稳定版,将zip文件解压,放到某个指定目录,cd进入这个目录,执行以下命令即可java -cp clojure-1.5.0.jar clojure.main。...当今最主流的运算平台JVM,把函数式编程语言引入JVM也是新方向。
Clojure是一个JVM的动态函数式语言,最近发布了1.0版本,版本中Clojure提供了一个健壮的代码。Clojure是一套发展快速的新的编程语言,特别是,它为多核计算做了新的解决方案。在今后的软件设计中无论使用Clojure语言...
clojure-cheatsheet, 用于Emacs的Clojure Cheatsheet 用于Emacs的难以置信方便的 Clojure Cheatsheet,更新为 Clojure,打包成简单,快速,可以搜索的离线形式: 状态准备使用基于 Clojure 1.7.0.安装如果你连接到 ...
clojure-csv, 从Clojure读取和写入CSV文件的库 csvclojure CSV是一个用于读取和写入CSV文件的小型库。 主要功能:接受两个常见行终止符。CSV字段内的引号和转义符是正确的句柄。解析时支持在CSV字段中嵌入未转义的行...
clojure 1.8.0
java运行依赖jar包
Clojure是一种LISP风格的语言,运行在JVM上。Clojure的一大特色就是其并发机制,它支持不可变的数据结构(Clojure是来自于可持久化的数据结构)。Clojure还有一个特色是软件事务存储(Software Transactional Memory...
Clojure的。 采用类似于草书的方法静态分析代码。 •••••• 总览 该项目的目标是为所有编辑人员带来适用于Clojure的出色编辑工具。 它旨在与您一起工作,以帮助您导航,识别和修复错误,执行重构等等! 你会...
clojure-sha-3-源码.rar
clojure-must-watch-源码.rar
Fork of http://code.google.com/p/clojure-jsr223/ that loads the Clojure runtime lazily. Copyright (c) 2009 Armando Blancas. All rights reserved. The use and distribution terms for this software are ...
Clojure入门教程- Clojure – Functional Programming for the JVM中文版
clojure-basics-源码.rar
clojure-style-guide:Clojure编程语言的社区编码风格指南
clojure-1.6 java jvm
Scala与Clojure函数式编程 Functional.Programming.Patterns.in.Scala.and.Clojure
clojure-tensorflow, Java api的Java上的一个极轻层 和 TensorFlowJava互操作上的包装器层,用于处理 TensorFlow 。 正在启动神经网络示例(ns example.core (:require [clojure-tensorflow.ops :a