[英]How to get a request object in Freemarker
在Velocity模板引擎中,我只能使用模型變量
$request
這是HttpServletRequest的實例。 如何在Freemarker模板引擎中獲取http請求對象? 根據freemarker文檔,
http://freemarker.org/docs/api/freemarker/ext/servlet/HttpRequestHashModel.html#getRequest--
有一個HttpRequestHashModel類,其方法返回HttpServletRequest的實例。
所以問題是,如何在Spring Boot中訪問該對象? 我發現了一些有關使用
${Request}
變量,但是我收到一個錯誤,它返回一個null / missing對象。
據我所知,Spring不會直接將請求公開給模板,但是默認情況下,它確實公開了模型屬性springMacroRequestContext ,該屬性包含有關請求的許多信息。
springMacroRequestContext變量允許您獲取有關請求的信息。
例如:
<html lang="${springMacroRequestContext.locale.language}" class="no-js">
要么
${springMacroRequestContext.contextPath}
根據您對獲取路徑的要求:
${springMacroRequestContext.requestUri}
應該足夠了。
有關所有可用方法,請參見org.springframework.web.servlet.support.RequestContext 。
您可以通過在application.properties中設置以下屬性來更改此屬性的名稱:
spring.freemarker.request-context-attribute=rc
這使您可以縮短模板中的語法:
${rc.locale}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.