# How to get query string parameters in Thymeleaf

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Jan 29, 2020, updated Sep 10, 2022. Topics: Thymeleaf.
Canonical URL: https://attacomsian.com/blog/thymeleaf-get-query-string-parameters

> A quick article to learn how to get query string parameters in Thymeleaf templates.

[Thymeleaf](https://attacomsian.com/blog/spring-boot-thymeleaf-example) 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:

```http
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](https://attacomsian.com/blog/thymeleaf-get-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:

```markup
<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:

```http
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:

```markup
<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:

```markup
<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.
