How to get query string parameters in Thymeleaf

Thymeleaf is a popular server-side template engine for Java-based web applications. In this quick article, you'll learn how to access query string parameters in Thymeleaf templates.

Query string parameters are commonly passed from the client to server as part of the base URL like:

https://example.com/search?q=thymeleaf&count=10&order=desc

In the above example URL, everything after ? is a part of the query string. There are three query string parameters (q, count, and order) included in the above URL.

Accessing Query String Parameters

Similar to session attributes, query string parameters can also be accessed in Thymeleaf views. For example to access the above query string parameters in Thymeleaf, you can use the param. prefix as shown below:

<div th:text="${param.q}">Search Query</div>
<div th:text="${param.count}">Results Per Page</div>
<div th:text="${param.order}">Results Ordering</div>

In the above example, if any parameter is missing, an empty string will be displayed otherwise the value of the parameter will be shown.

Multi-valued Request Parameters

Since query string parameters can be multivalued, accessing them like above may return a serialized array as a value. Here is an example URL that contains a multivalued query string parameter:

https://example.com/search?q=thymeleaf&fields=id&fields=name&fields=email

For such a scenario, you have to use the bracket syntax to access the parameter values:

<p>Field 1: <span th:text="${param.fields[0]}"></span></p>
<p>Field 2: <span th:text="${param.fields[1]}"></span></p>
<p>Field 3: <span th:text="${param.fields[2]}"></span></p>

Another way to access query string parameters is by using the special #request object that gives you direct access to the javax.servlet.http.HttpServletRequest object:

<p th:text="${#request.getParameter('q')}">Text</p>

Conclusion

In this short article, we looked at different ways to access query string parameters in a Thymeleaf template. You can either use the param. prefix or the #request object to easily access single as well as multivalued query string parameters.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.