我从事 Clojure 项目已有一段时间了,想知道如何在单击提交按钮后将用户从一个页面导航到另一个页面。 代码是这样的:
(defn view-film-input []
(view-layout
[:body {:style "background-color: #F2FB78"}
[:h2 "Add new film"]
(form-to [:post "/addfilm" ]
(label "movname" "Film title: ")
(text-field :txtname) [:br]
(label "prname" "Producer name: ")
(text-field :txtprname) [:br]
(label "location" "File location: ")
(text-field :txtlocation)[:br]
(label "duration" "Duration(minutes): ")
(text-field :txtduration)[:br]
(submit-button "Save"))]))
现在,这有效,但我想将用户导航到相同的“添加新电影”页面,或者在单击“保存”按钮后刷新表单,而不是它只显示一个空白页面。
这是 GET\POST 部分:
(GET "/addfilm" [] (view-film-input))
(POST "/addfilm" [txtname txtprname txtlocation txtduration]
(insert-flick txtname txtprname txtlocation txtduration 90) )
提前致谢!
最佳答案
这里有两种可能性。
重定向用户
使用 HTTP 302(或 303)响应的 Location
header ,您可以指定用户浏览器应显示的路径而不是当前路径:
(POST "/addfilm" [...]
...
{:status 302
:headers {"Location" "/addfilm"}})
ring.util.response
中还有两个函数可以为您生成响应:redirect
和 redirect-after-post
- 后者更适用于您的用例。
编辑: This answer详细说明了为什么 303 是在 POST 之后发送的状态码,具体来说:
If you use 302, you're risking that UA will re-send POST to the new URL instead of switching to GET.
再次渲染同一个页面
更简单并提示更少的 I/O,但强加一些重复将再次呈现相同的 View ,即:
(POST "/addfilm" [...]
...
(view-film-input))
这对我来说似乎不太容易维护,但它可能是解决您问题的最短途径。
关于forms - 如何使用 Clojure 将链接放入提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25574823/