我使用Clojure + Ring构建在Glassfish 3上运行的Web应用程序。
如何访问Ring ServletContext
函数中的init
变量?
答案 0 :(得分:1)
ServletContext(如果有)在请求映射中可用。我发现查看:context, :servlet-context
和:servlet-context-path
的值非常有用。这是我用来确定路径的小环中间件:
(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))