2014-12-08 25 views
3

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

+2

到目前为止,您有什么尝试?请阅读[我如何问一个好问题?](http://stackoverflow.com/help/how-to-ask)。 – DavidPostill 2014-12-08 08:14:30

回答

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)) 
+1

不幸的是'init'函数不能访问'request'参数。 – Amigo 2014-12-08 10:14:55

+0

我看到你正在确定路径,但实际上并没有调用url-in-context。不应该从wrap-context调用url-in-context(即,如果您确实希望您的应用能够透明地工作而不管它在哪里)? – user3810626 2016-08-11 00:08:55

+0

否,应用程序中会使用'url-in-context',例如,在视图模型中构建链接。 – schaueho 2016-08-11 16:22:23

相关问题