th:action is a Thymeleaf attribute that sets the action of an HTML <form> from a link expression such as @{/orders}. Thymeleaf evaluates the expression, adds the application context path, applies URL rewriting, and, in a Spring application, lets Spring add hidden fields such as the CSRF token.
This makes the form work no matter where the application is deployed, which a hard-coded action="/orders" does not.
@{...}Thymeleaf is a server-side template engine. Attributes with the th: prefix are processed before the HTML is sent to the browser, and the processed attribute replaces the static one. For th:action, the result is written to the standard action attribute:
If the application runs under the context path /shop, the browser receives:
The template remains valid HTML when you open it directly in a browser, because an unknown th:action attribute is ignored and the plain action attribute can serve as a prototype value.
@{...}th:action almost always takes a link expression. The syntax @{...} supports several forms:
| Expression | Result | Use |
|---|---|---|
@{/orders} | /ctx/orders | Context-relative URL |
@{orders} | orders | Relative to the current page |
@{~/other-app/home} | /other-app/home | Another application on the same server |
@{https://example.com/pay} | unchanged | Absolute URL |
@{/orders/{id}(id=${order.id})} | /ctx/orders/42 | Path variable |
@{/search(q=${term},page=1)} | /ctx/search?q=java&page=1 | Query parameters |
Parameter values are URL-encoded automatically, so you do not need to encode them yourself.
In a Spring MVC application, th:action is usually combined with th:object and th:field. The controller puts an empty or existing object in the model, and the template binds the inputs to its properties.
th:field generates the id, name and value attributes from the bound property, and th:errors prints validation messages from the BindingResult.
When Spring Security is on the classpath and CSRF protection is enabled (the default for browser applications), Thymeleaf's Spring integration adds a hidden input to every form that uses th:action:
This only happens with th:action. If you write a plain action="/customers" attribute, the token is missing and a POST request fails with a 403 response. Using th:action on every form is the simplest way to avoid that mistake.
Browsers only submit GET and POST from a form. To reach a @PutMapping or @DeleteMapping, send a hidden _method field and enable Spring's HiddenHttpMethodFilter. In Spring Boot it is off by default; turn it on in application.properties:
action with th:action="@{/...}".th:action, or add the CSRF hidden field yourself.Neither BindingResult nor plain target object when rendering. The controller did not add the object named in th:object to the model.th:action value of ${...} instead of @{...}. A variable expression does not add the context path, so use a link expression unless you intend to supply the full URL.It is still recommended. It keeps the form correct if the context path changes later, and it enables the automatic CSRF field with Spring Security.
Both use link expressions. th:action sets the action attribute on <form>, while th:href sets the href attribute on <a> and <link> elements.
The method attribute is plain HTML and usually written as method="post". Thymeleaf does not rewrite it, which is why PUT and DELETE need the hidden _method field.
Yes, as Thymeleaf is a standalone engine, but the automatic CSRF field and th:object binding are provided by the Thymeleaf Spring integration.
th:action turns a link expression into the form's action attribute, so URLs include the context path, are encoded correctly and stay valid across deployments. In Spring applications it also triggers hidden fields such as the CSRF token. Use it on every server-rendered form, together with th:object and th:field for binding.