Partner – Microsoft – NPI (cat= Spring)
announcement - icon

Azure Spring Apps is a fully managed service from Microsoft (built in collaboration with VMware), focused on building and deploying Spring Boot applications on Azure Cloud without worrying about Kubernetes.

And, the Enterprise plan comes with some interesting features, such as commercial Spring runtime support, a 99.95% SLA and some deep discounts (up to 47%) when you are ready for production.

>> Learn more and deploy your first Spring Boot app to Azure.

You can also ask questions and leave feedback on the Azure Spring Apps GitHub page.

1. Overview

In this short tutorial, we’ll show how to get URL attributes in the Thymeleaf views.

2. How to Get URL Parameter Attributes

Accessing URL attributes, or request parameters, can be easily done in Thymleaf views using one of two special Thymleaf objects. The first way is to use the param object, and the second is to use the #request object.

For demonstration purposes, let’s consider a URL that holds one parameter, query:

https://baeldung.com/search?query=Baeldung

2.1. Using the param Object

First, let’s see how to use the param object to access the URL attribute “query”:

<div th:if="${param.query != null}">
    <p th:text="${param.query }"></p>
</div>

In the above example, if the parameter “query” isn’t null, the value of “query” will be shown. Also, we should note that URL attributes can be multivalued. Let’s see an example URL with a multivalued attribute:

https://baeldung.com/search?query=Bealdung&query=Thymleaf

In this case, we can access the values separately using brackets syntax:

<div th:if="${param.query != null}">
    <p th:text="${param.query[0] + ' ' + param.query[1]}" th:unless="${param.query == null}"></p>
</div>

2.2. Using the request Object

Next, let’s see the second way to access URL attributes. We can use the special #request object that gives you direct access to the javax.servlet.http.HttpServletRequest object, which breaks a request down into parsed elements such as query attributes and headers.

Let’s see how to use the #request object in a Thymleaf view:

<div th:if="${#request.getParameter('query') != null}">
    <p th:text="${#request.getParameter('query')}" th:unless="${#request.getParameter('query') == null}"></p>
</div>

In the above example, we used the special function getParameter(‘query’) offered by the #request object. This method returns the value of a request parameter as a String or null if the parameter does not exist.

3. Conclusion

In this quick article, we explained how to get URL attributes in the Thymeleaf views using param and #request objects. As always, the code snippets are available over on GitHub.

Course – LS (cat=Spring)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.