Thymeleaf 絕對路徑


在網上搜索到的大部分解決方案都是[1] 

<a th:href="${#httpServletRequest.getScheme() + '://' + #httpServletRequest.getServerName() + ':' + #request.getServerPort()  + #request.getContextPath() + @{/login}} ">點我啊!</a>

其中:

  • getScheme()獲取協議
  • getServerName()獲取服務器名
  • getServerPort()服務器端口
  • getContextPath() APP根路徑

然而將代碼復制修改后的效果是:

前台報錯

 

 后台報錯

 

 

org.springframework.expression.spel.SpelParseException: Expression [#httpServletRequest.getScheme() + '://' + #httpServletRequest.getServerName() + ':' + #request.getServerPort()  + #request.getContextPath() + @{/login}] @142: EL1059E: @ or & can only be followed by an identifier or a quoted name

 

 根據報錯信息,對方案略微修改:

@{/admin} 修改為 '/admin'

<a th:href="${#httpServletRequest.getScheme() + '://' + #httpServletRequest.getServerName() + ':' + #request.getServerPort()  + #request.getContextPath() + '/admin'}">跳轉主頁</a>

 

 

 Reference:

[1] sayyy, 【Thymeleaf】獲取絕對路徑, https://blog.csdn.net/sayyy/article/details/80858392?utm_source=blogkpcl3

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM