我正在使用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