Clojure 如何在环初始化函数中获取ServletContext
我正在使用Clojure+Ring构建一个运行在GlassFish3上的web应用程序。Clojure 如何在环初始化函数中获取ServletContext,clojure,glassfish,ring,Clojure,Glassfish,Ring,我正在使用Clojure+Ring构建一个运行在GlassFish3上的web应用程序。 如何访问环init函数中的ServletContext变量?请求映射中提供了ServletContext(如果有)。我发现查看:context、:servlet context和:servlet context path的值很有用。下面是一个用于确定路径的小型环形中间件: (def ^:dynamic *app-context* nil) (defn wrap-context [handler] (fn
如何访问环
init
函数中的ServletContext
变量?请求映射中提供了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))
到目前为止你试过什么?请阅读。不幸的是,
init
函数无权访问request
参数。我看到您正在确定路径,但实际上并没有在上下文中调用url。是否应该从包装上下文调用上下文中的url(即,如果你真的希望你的应用程序无论在哪里都能透明地工作)?否,url-in-context
将用于应用程序中,例如在视图模型中构建链接。