我已经使用noir框架创建了一个用于clojure的web项目,并且想要尝试使用pre-route宏。
这是我的test.clj
(ns website.views.test
(:require [website.views.common :as common] [noir.content.getting-started])
(:use [noir.core :only [defpage]])
(:use [hiccup.core :only [html]])
(:require noir.core))
(defn is-admin? [] false)
(pre-route '/admin/*' (when-not (is-admin?) (redirect '/login')))
(defpage "/login" [] (common/layout [:p "login to website"]))
(defpage "/admin" [] (common/layout [:p "Welcome to admin page"]))
(defpage "/admin/test1" [] (common/layout [:p "Welcome to admin test1 page"]))
(defpage "/admin/test2" [] (common/layout [:p "Welcome to admin test2"]))
执行'lein run'时,我得到以下堆栈跟踪
Caused by: java.lang.RuntimeException: Invalid token: /admin/*'
at clojure.lang.Util.runtimeException(Util.java:170)
at clojure.lang.LispReader.interpretToken(LispReader.java:321)
at clojure.lang.LispReader.read(LispReader.java:206)
at clojure.lang.LispReader$WrappingReader.invoke(LispReader.java:537)
at clojure.lang.LispReader.readDelimitedList(LispReader.java:1126)
at clojure.lang.LispReader$ListReader.invoke(LispReader.java:962)
at clojure.lang.LispReader.read(LispReader.java:180)
at clojure.lang.Compiler.load(Compiler.java:6949)
... 51 more
知道这个异常的原因是什么(当我注释掉路由前的宏时不会发生这种情况)?
答案 0 :(得分:2)
(pre-route '/admin/*' (when-not (is-admin?) (redirect '/login')))
应该是:
(pre-route "/admin/*" (when-not (is-admin?) (redirect "/login")))
因为'
是引用的读者宏。