Spring MVC:如何在 Thymeleaf 中获取当前 url [英] Spring MVC: How do I get current url in Thymeleaf

查看:63
本文介绍了Spring MVC:如何在 Thymeleaf 中获取当前 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Spring Web MVC 中使用 Thymeleaf 模板引擎在当前 url 的帮助下创建 url 时卡住了.有没有办法在 Thymeleaf HTML 文件中获取最新信息?例如:假设我在浏览器地址栏中的当前 url 是:

I am using Thymeleaf Template Engine with Spring Web MVC and I am got stuck while creating url's with the help of current url. Is there any way to get current inside Thymeleaf HTML file? eg: Suppose my current url in my browser address bar is:

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

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

and now I want to make a url like this http://localhost:8080/project/web/category/mobiles/store/samsung

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

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

So I the code will look like this

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

这里我使用带有硬编码 url 的 currentUrl 变量,所以我想要一些相同的解决方案.硬编码值不会每次都有效,因为我有动态类别.

Here I am using currentUrl variable with hardcoded url, So I want to some solution for the same. The hardcoded value will not work everytime because I have dynamic categories.

我对相对 url 进行了相同的尝试,但对我不起作用.

I tried the same with relative url but its not working for me.

<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

请看一看,如果我做错了什么,请告诉我.

Please have a look and let me know if am I doing something wrong.

推荐答案

哦,我找到了解决方案.我错过了文档中的 {#httpServletRequest.requestURI}.

Oh I got the solution for this. I missed the {#httpServletRequest.requestURI} in the documentation.

这是对我有用的解决方案:

Here is the solution which is working for me:

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

这篇关于Spring MVC:如何在 Thymeleaf 中获取当前 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆