一个完整的示例如下所示:
(ns myapp.routes.home
(:use [hiccup core form])
(:require [compojure.core :refer :all]))
(defn quick-form [& [name message error]]
(html
(form-to {:enctype "multipart/form-data"}
[:post "/form-out"]
(text-field "Hello")
(submit-button {:class "btn" :name "submit"} "Save")
(submit-button {:class "btn" :name "submit"} "Clone"))))
请注意,对两个提交按钮使用相同的名称允许您在结果映射中简单地查找“提交”键。
(defroutes home-routes
(GET "/form-in" [] (quick-form))
(POST "/form-out" [:as request] (str (request :multipart-params))))
打开以下页面时:
http://localhost:3000/form-in
并填写表格,POST路线的结果是:
{"submit" "Save", "Hello" "hello2"}
顺便说一句,我发现了一篇关于请求映射在 Compojure 中的结构方式的有用的旧帖子,因此它可以更容易地在 Clojure 代码中对其进行解构。