【Thymeleaf】获取绝对路径
生活随笔
收集整理的這篇文章主要介紹了
【Thymeleaf】获取绝对路径
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
前言
- thymeleaf 3.0
獲取絕對路徑
使用Thymeleaf模板引擎想要拼出絕對路徑
<a th:href="${#request.getScheme() + '://' + #request.getServerName() + ':' + #request.getServerPort() + @{/login}} ">點我啊!</a>等效的寫法:
<a th:href="${#httpServletRequest.getScheme() + '://' + #httpServletRequest.getServerName() + ':' + #httpServletRequest.getServerPort() + @{/login}} ">點我啊!</a>解釋下:
Inside a web environment there is also direct access to the following objects (note these are objects, not maps/namespaces):
#request : direct access to the javax.servlet.http.HttpServletRequest object associated with the current request.
按照官方文檔的說法,內(nèi)置變量 #request 等同于 javax.servlet.http.HttpServletRequest。
因此,可以按照 javax.servlet.http.HttpServletRequest 拼接URL的方式,拼接絕對路徑。
javax.servlet.http.HttpServletRequest 的方法說明
- getScheme() 獲取協(xié)議
- getServerName() 獲取服務(wù)器名
- getServerPort() 服務(wù)器端口
- getContextPath() APP根路徑
勘誤
之前給出的寫法有些問題,已修正。
下面是之前給出的錯誤的寫法:
<a th:href="${#httpServletRequest.getScheme() + '://' + #httpServletRequest.getServerName() + ':' + #request.getServerPort() + #request.getContextPath() + @{/login}} ">點我啊!</a>參考
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#appendix-a-expression-basic-objects
總結(jié)
以上是生活随笔為你收集整理的【Thymeleaf】获取绝对路径的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【thymeleaf】【SpringBo
- 下一篇: 稀汤寡水(稀汤寡水打一动物)