我在获取以下Compojure示例中的表单参数时遇到问题:
(ns hello-world
(:use compojure.core, ring.adapter.jetty)
(:require [compojure.route :as route]))
(defn view-form []
(str "<html><head></head><body>"
"<form method=\"post\">"
"Title <input type=\"text\" name=\"title\"/>"
"<input type=\"submit\"/>"
"</form></body></html>"))
(defroutes main-routes
(GET "/" [] "Hello World")
(GET "/new" [] (view-form))
(POST "/new" {params :params} (prn "params:" params))
(route/not-found "Not Found"))
(run-jetty main-routes {:port 8088})提交表单时,输出始终为
params: {}我不明白为什么title参数不在params映射中。
我使用的是Compojure 0.6.2。
发布于 2011-05-18 05:22:16
你是否考虑到了这一点:
从0.6.0版本开始,
不再向路由添加默认中间件。这意味着您必须显式地将wrap-params和wrap-cookies中间件添加到路由中。
来源:https://github.com/weavejester/compojure
我用我当前的设置尝试了你的例子,它起作用了。我已经包含了以下内容:require [compojure.handler :as handler]和(handler/api routes)。
发布于 2011-11-20 11:54:27
这是一个很好的例子,说明了如何处理参数
(ns example2
(:use [ring.adapter.jetty :only [run-jetty]]
[compojure.core :only [defroutes GET POST]]
[ring.middleware.params :only [wrap-params]]))
(defroutes routes
(POST "/" [name] (str "Thanks " name))
(GET "/" [] "<form method='post' action='/'> What's your name? <input type='text' name='name' /><input type='submit' /></form>"))
(def app (wrap-params routes))
(run-jetty app {:port 8080})https://github.com/heow/compojure-cookies-example
请参见示例2-中间件的功能
发布于 2011-05-18 04:43:01
您可以只提供一个参数列表;compojure会相应地自动将它们从POST/GET参数中获取出来。如果你需要做更复杂的事情,你可以做,但我从来没有研究过如何做。例如,下面是4clojure的代码片段
(POST "/problems/submit" [title tags description code]
(create-problem title tags description code))https://stackoverflow.com/questions/6036733
复制相似问题