我正在使用Clojure+Ring构建一个运行在Glassfish 3上的Web应用程序。
如何在Ring的init函数中访问ServletContext变量?
如果有的话,ServletContext可以在请求映射中使用。我发现查看、和值很有用。这是我使用的一个小型ring中间件,用于确定路径:
(def ^:dynamic *app-context* nil)
(defn wrap-context [handler]
(fn [request]
(when-let [context (:context request)]
(logging/debug (str "Request with context " context)))
(when-let [pathdebug (:path-debug request)]
(logging/debug (str "Request with path-debug " pathdebug)))
(when-let [servlet-context (:servlet-context request)]
(logging/debug (str "Request with servlet-context " servlet-context)))
(when-let [servlet-context-path (:servlet-context-path request)]
(logging/debug (str "Request with servlet-context-path " servlet-context-path)))
(binding [*app-context* (str (:context request) "/")]
(logging/debug (str "Using appcontext " *app-context*))
(-> request
handler))))
(defn url-in-context [url]
(str *app-context* url))
init
函数无法访问request
参数。 - Amigourl-in-context
将在应用程序中使用,例如在视图模型中构建链接。 - schaueho