Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

In our article Introduction to Using Thymeleaf in Spring, we saw how to bind user input to objects.

We used th:object and th:field in the Thymeleaf template and @ModelAttribute in the controller to bind data to a Java object. In this article, we’ll look at how to use the Spring annotation @RequestParam in combination with Thymeleaf.

2. Parameters in Forms

Let’s first create a simple controller that accepts four optional request parameters:

@Controller
public class MainController {
    @RequestMapping("/")
    public String index(
        @RequestParam(value = "participant", required = false) String participant,
        @RequestParam(value = "country", required = false) String country,
        @RequestParam(value = "action", required = false) String action,
        @RequestParam(value = "id", required = false) Integer id,
        Model model
    ) {
        model.addAttribute("id", id);
        List<Integer> userIds = asList(1,2,3,4);
        model.addAttribute("userIds", userIds);
        return "index";
    }
}

The name of our Thymeleaf template is index.html. In the following three sections, we’ll use different HTML form elements for the user to pass data to the controller.

2.1. Input Element

First, let’s create a simple form with a text input field and a button to submit the form:

<form th:action="@{/}">
<input type="text" th:name="participant"/> 
<input type="submit"/> 
</form>

The attribute th:name=”participant” binds the value of the input field to the parameter participant of the controller. For this to work, we need to annotate the parameter with @RequestParam(value = “participant”).

2.2. Select Element

Likewise for the HTML select element:

<form th:action="@{/}">
    <input type="text" th:name="participant"/>
    <select th:name="country">
        <option value="de">Germany</option>
        <option value="nl">Netherlands</option>
        <option value="pl">Poland</option>
        <option value="lv">Latvia</option>
    </select>
</form>

The value of the selected option is bound to the parameter country, annotated with @RequestParam(value = “country”).

2.3. Button Element

Another element where we can use th:name is the button element:

<form th:action="@{/}">
    <button type="submit" th:name="action" th:value="in">check-in</button>
    <button type="submit" th:name="action" th:value="out">check-out</button>
</form>

Depending on whether the first or second button is pressed to submit the form, the value of the parameter action will be either check-in or check-out.

Another way to pass request parameters to a controller is via a hyperlink:

<a th:href="@{/index}">

And we can add parameters in parentheses:

<a th:href="@{/index(param1='value1',param2='value2')}">

Thymeleaf evaluates the above to:

<a href="/index?param1=value1&param2=value2">

Using Thymeleaf expressions to generate hyperlinks is particularly useful if we want to assign parameter values based on variables. For example, let’s generate a hyperlink for each user ID:

<th:block th:each="userId: ${userIds}">
    <a th:href="@{/(id=${userId})}"> User [[${userId}]]</a> <br/>
</th:block>

We can pass a list of user IDs as a property to the template:

List<Integer> userIds = asList(1,2,3);
model.addAttribute("userIds", userIds);

And the resulting HTML will be:

<a th:href="/?id=1"> User 1</a> <br/>
<a th:href="/?id=2"> User 2</a> <br/>
<a th:href="/?id=3"> User 3</a> <br/>

The parameter id in the hyperlink is bound to the parameter id, annotated with @RequestParam(value = “id”).

4. Summary

In this short article, we saw how to use Spring request parameters in combination with Thymeleaf.

First, we created a simple controller that accepts request parameters. Second, we looked at how to use Thymeleaf to generate an HTML page that can call our controller.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW