Spring MVC: How do I get current url in Thymeleaf
Asked Answered
H

5

35

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

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

or

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>

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.

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.

Hinton answered 5/5, 2014 at 6:25 Comment(2)
Why do you need the full URL? Why not simply use the relative URL?Arbitress
In my case http://localhost:8080/project is my base url and /web/category/{categoryName} is mapped by controller. I tried the relative but it doesn't seems to work, I am using this for relative urls <a th:href="@{/store/__${store.name}__}>" Click to More</a>.Hinton
H
47

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
Hinton answered 5/5, 2014 at 7:9 Comment(5)
${#httpServletRequest.requestURI} works just fine (Thymeleaf Spring4 2.1.5)Hanselka
In my application, requestURI give me something like this: [/store/something] while requestURL give me something like this: [example.com/store/something]. I opt for requestURL since complete url is better for my use case.Martens
For the future readers; you can use ${#request.requestURI} in Thymeleaf 3.0(Link)Typhoeus
Note that #httpServletRequest.requestURI doesn't include query string or fragment.Stearin
Thymeleaf 3.1 removed #httpServerRequestSurtout
S
32

You can get the current URL using two single ' quotes. Example:

<a th:href="@{''(lang=de)}">Deutsch</a>

Note that this unfortulately does not include existing URL parameters.

Sneaker answered 27/12, 2016 at 10:18 Comment(4)
I just now tried again and it works... : <a th:href="@{''}">Current Page</a>. What is your Thymeleaf version? I use 3.0.2.Sneaker
Didn't work for me either. I'm using 3.0.3; your example renders this <a href="">Current Page</a>.Garnettgarnette
Nice solution. Works here with Spring 5.0.4, Spring Boot 2.0, Thymeleaf 3.0.9Phallus
Note that, while this works, it doesn't return a full url; it generates relative urlsStepchild
S
10

In Thymeleaf 3.1, the current URL can be obtained from the context object:

<form th:action="${#ctx.springRequestContext.requestUri}">
Stodgy answered 26/12, 2022 at 17:30 Comment(3)
Still valid with Spring-Boot 3.1.1Fideicommissum
not valid anymore in with spring boot 3.1.5. I don't understand why this feature is removed. Whould be great to explain this change. I assume the thymleaf team as a great reason to do so.Colly
here is the explanation: github.com/thymeleaf/thymeleaf/issues/886Colly
A
3

If the requested url is for example : http://localhost:8080/my-page

I could get this value: /my-page using this: httpServletRequest.requestURI

For example, if you have to load some resource , you can use:

<script th:src="@{|${#httpServletRequest.requestURI}/main.js|}"></script>

And renderized html will be:

<script src="/my-page/main.js"></script>
Alopecia answered 9/7, 2019 at 23:36 Comment(1)
Thymeleaf 3.1 removed #httpServerRequestSurtout
S
3

As of Thymeleaf 3.1 there appears to be no way to access the current request by default.

You will need to add it manually to the context, for example:

@ModelAttribute("currentUrl")
public String getCurrentUrl(HttpServletRequest request) {
    return request.getRequestURI();
}
<a th:href="@{__${currentUrl}__/__${store.name}__}">
    Click to More Result
</a>
Surtout answered 24/1 at 11:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.