在網上搜索到的大部分解決方案都是[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