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