slh*_*sen 6 clojure compojure ring http-headers
我正在尝试通过实现一个简单的Web应用程序开始使用Clojure和Clojurescript.到目前为止事情进展顺利,从不同的教程中读到我提出的代码如下:
core.clj:
(ns myapp.core
(:require [compojure.core :as compojure]
[compojure.handler :as handler]
[compojure.route :as route]
[myapp.controller :as controller]))
(compojure/defroutes app-routes
(compojure/GET "/" [] controller/index)
(route/resources "/public")
(route/not-found "Not Found"))
(def app
(handler/site app-routes))
Run Code Online (Sandbox Code Playgroud)
controller.clj:
(ns myapp.controller
(:use ring.util.response)
(:require [myapp.models :as model]
[myapp.templates :as template]))
(defn index
"Index page handler"
[req]
(->> (template/home-page (model/get-things)) response))
Run Code Online (Sandbox Code Playgroud)
templates.clj:
(ns myapp.templates
(:use net.cgrand.enlive-html)
(:require [myapp.models :as model]))
(deftemplate home-page "index.html" [things]
[:li] (clone-for [thing things] (do->
(set-attr 'data-id (:id thing))
(content (:name thing)))))
Run Code Online (Sandbox Code Playgroud)
问题是我无法在页面上显示非ascii字符,我不知道如何在页面上设置HTTP标头.
我看到这样的解决方案,但我根本无法弄清楚它们放在我的代码中的位置:
(defn app [request]
{:status 200
:headers {"Content-Type" "text/plain"}
:body "Hello World"})
Run Code Online (Sandbox Code Playgroud)
PS:欢迎任何有关样式和/或代码组织的建议.
Gui*_*ler 11
用途ring.util.response:
(require '[ring.util.response :as r])
Run Code Online (Sandbox Code Playgroud)
然后在你的index功能:
(defn index
"Index page handler"
[req]
(-> (r/response (->> (template/home-page (model/get-things)) response))
(r/header "Content-Type" "text/html; charset=utf-8")))
Run Code Online (Sandbox Code Playgroud)
您可以在响应上链接其他操作,例如set-cookie和whatnot:
(defn index
"Index page handler"
[req]
(-> (r/response (->> (template/home-page (model/get-things)) response))
(r/header "Content-Type" "text/html; charset=utf-8")
(r/set-cookie "your-cookie-name"
"" {:max-age 1
:path "/"})))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5233 次 |
| 最近记录: |