如何在ring init函数中获取ServletContext

时间:2014-12-08 02:30:37

标签: clojure glassfish ring

我使用Clojure + Ring构建在Glassfish 3上运行的Web应用程序。 如何访问Ring ServletContext函数中的init变量?

1 个答案:

答案 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))