Thymeleaf using path variables to thhref
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Thymeleaf, path variables inside th:href are created by using a URI template and then supplying values in parentheses. The syntax is compact once you know it, but it is easy to confuse with query-parameter syntax if you have only seen simpler @{/path} examples.
The Core Syntax
The basic pattern is:
This produces a URL such as /users/42 when user.id is 42.
The rule is:
- put the path variable placeholder in braces inside the path
- provide the replacement value in parentheses after the path
That is how Thymeleaf maps model data into path variables.
A Complete Example
Controller:
Template:
Rendered result:
That is the normal Thymeleaf pattern for Spring MVC path-variable links.
Multiple Path Variables
You can provide more than one variable.
This becomes something like /teams/7/users/42.
The names in the parentheses must match the placeholders in the path template.
Path Variables vs Query Parameters
Do not confuse this with query-parameter syntax. Query parameters are written like this:
That produces /users?id=42, not /users/42.
So if you want REST-style path segments, keep the placeholder inside the path itself.
When Context Paths Matter
Thymeleaf URL expressions are context-aware. If your application is deployed under a context path such as /app, the generated link will reflect that automatically. That is one reason to prefer th:href over manually concatenating strings.
String concatenation works, but it is more fragile and gives up Thymeleaf's URL handling.
This matters because many Spring MVC routes are designed around path variables. Using the matching Thymeleaf syntax keeps the template aligned with the controller mapping instead of manually stitching URL strings together.
Common Pitfalls
The biggest mistake is putting the variable only in the parentheses and forgetting the placeholder in the path. That generates a query parameter, not a path variable.
Another mistake is mismatching the placeholder name and the variable name in the parameter list.
A third mistake is building URLs by string concatenation instead of using Thymeleaf URL expressions. The template engine already solves this problem cleanly.
Summary
- Use
@{/path/{var}(var=${value})}for Thymeleaf path variables inth:href. - The placeholder goes in the path, and the value mapping goes in parentheses.
- Multiple path variables are supported by listing each mapping explicitly.
- Query-parameter syntax is different and produces a different URL shape.
- Prefer Thymeleaf URL expressions over manual string concatenation for links.
Related reading
- Time complexity of System.arraycopy...?
- Timed annotation in spring metrics
- Timing out the execution time of a controller/method in Spring
- Tips for using Vim as a Java IDE?
- Tips implementing permutation algorithm in Java
- .toArraynew MyClass0 or .toArraynew MyClassmyList.size?
- Tomcat How to find out running Tomcat version?
- Tomcat takes too much time to start - Java SecureRandom

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.