Spring MVC:如何在Thymeleaf中获取当前URL


问题内容

我正在将Thymeleaf模板引擎与Spring Web
MVC一起使用,并且在使用当前URL的帮助创建URL时卡住了。有什么方法可以让Thymeleaf
HTML文件中的当前文件吗?例如:假设我当前在浏览器地址栏中的网址是:

http://localhost:8080/project/web/category/mobiles

现在我想这样的网址 http://localhost:8080/project/web/category/mobiles/store/samsung

要么

http://localhost:8080/project/web/category/mobiles?min_price=10&max_price=100

所以我的代码看起来像这样

<a th:with="currentUrl='http://localhost:8080/project/web/category/mobiles'" 
   th:href="@{__${currentUrl}__/__${store.name}__}">
    Click to More Result
</a>

在这里,我将currentUrl变量与硬编码的url一起使用,因此我想要一些相同的解决方案。硬编码的值不会每次都起作用,因为我有动态类别。

我用相对网址尝试了同样的方法,但是它对我不起作用。

<a th:href="@{/store/__${store.name}__}">Click to More</a>

//will produce: http://localhost:8080/project/web/store/samsung
//I want: http://localhost:8080/project/web/category/mobiles/store/samsung

请看看,让我知道我做错了什么。


问题答案:

哦,我有解决办法。我错过{#httpServletRequest.requestURI}了文档中的。

这是为我工作的解决方案:

<a th:href="@{__${#httpServletRequest.requestURI}__/store/__${store.name}__}">Click to More</a>
//Will produce: http://localhost:8080/project/web/category/mobiles/store/samsung