百里香叶:在没有应用程序上下文路径的情况下获取 href



如何在没有上下文路径的情况下获取URL?我正在使用弹簧启动 1.5.18.发布和百里香叶

我尝试了这两种可能性。

<a th:href="@{${website}}" target="_blank"> Click here </a>
<a th:href="${website}" target="_blank"> Click here </a>

例如,如果 ${website} 具有值 www.example.com 我得到以下结果:

http://localhost:8081/MyApp/www.example.com但我想只得到这个没有上下文路径的 www.example.com。

我这样解决了:

<th:block th:with="link=${website}">
         <a th:href="@{//{link}(link=${link})}" target="_blank">&gt; Click here</a>
</th:block>

如果您要链接到外部站点,则应将http://https://作为链接的一部分 - https://www.example.com而不仅仅是www.example.com。 添加后,初始帖子中的两个解决方案中的任何一个都将起作用。

<a th:href="@{${website}}" target="_blank">Click here</a>
<a th:href="${website}" target="_blank">Click here</a>

//如您所指出的也可以工作,但这有点愚蠢。 这只是意味着,如果您的网站https://它,链接将被https://(http://反之亦然(。

如果您不想将https://添加到您的链接中,我会推荐这样的东西:

<a th:href="|https://${website}|" target="_blank">&gt; Click here</a>

你可以动态使用,它对我有用

<img th:src="${#httpServletRequest.scheme}+'://'+${#httpServletRequest.serverName}+':'+${#httpServletRequest.serverPort}+@{/user/display/${user.id}}" />

相关内容

最新更新