我正在使用Clojure + Ring構建運行在Glassfish 3上的Web應用程序。 如何才能訪問init
函數中的ServletContext
變量?如何獲取ServletContext中的ring init函數
回答
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'參數。 – Amigo 2014-12-08 10:14:55
我看到你正在確定路徑,但實際上並沒有調用url-in-context。不應該從wrap-context調用url-in-context(即,如果您確實希望您的應用能夠透明地工作而不管它在哪裏)? – user3810626 2016-08-11 00:08:55
否,應用程序中會使用'url-in-context',例如,在視圖模型中構建鏈接。 – schaueho 2016-08-11 16:22:23
- 1. 如何從AsyncContext獲取ServletContext?
- 2. Seam 2,如何獲取servletcontext?
- 3. 在ServiceLayer中獲取ServletContext
- 4. 如何使用init函數?
- 5. 如何獲取ring-clojure中的客戶端IP地址?
- 6. 如何在Clojure Compojure/Ring項目中獲取`HttpInputOverHTTP`的內容?
- 7. 獲取ServletContext連接到JSP
- 8. 如何解決Ring編程語言中的「無定義的調用函數!:init」錯誤?
- 9. 春季獲取工廠類的servletcontext
- 10. 如何在init中添加函數
- 11. 如何獲取其餘Web服務中的ServletContext
- 12. 如何從jsp獲取web.xml中servlet的init參數?
- 13. 在JAX-RS資源中獲取ServletContext
- 14. jQuery init()函數。
- 15. 如何讓OpenGlView的init函數運行?
- 16. Kotlin Array init函數
- 17. 從攔截器的MessageContext獲取ServletContext
- 18. 如何用函數更改ng-init值?
- 19. 如何從(init)JSON函數調用(ko.observableArray)?
- 20. 在Servlet 2.3的init方法中獲取上下文路徑(因此沒有可用的ServletContext#getContextPath())
- 21. 如何在函數中獲取id?
- 22. 如何從函數中獲取值
- 23. 如何在Wordpress函數中獲取Meta_key?
- 24. 如何從javascript函數中獲取值?
- 25. 如何從JavaScript函數中獲取值?
- 26. 如何從函數中獲取變量?
- 27. 如何從函數中獲取值?
- 28. 如何從另一個函數中獲取函數的結果?
- 29. 如何在PHP中獲取函數內部的函數值?
- 30. 如何獲取函數中的默認構造函數值
到目前爲止,您有什麼嘗試?請閱讀[我如何問一個好問題?](http://stackoverflow.com/help/how-to-ask)。 – DavidPostill 2014-12-08 08:14:30