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:

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:

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>


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.

Last Updated: