Thymeleaf中th:action与th:formaction的区别及表单提交问题咨询
th:form, th:action, and th:formaction Explained Hey there, fellow Thymeleaf newbie! I remember struggling with these exact attributes when I first started building Spring Boot apps with Thymeleaf views, so let me break this down clearly for you.
What’s th:form used for?
th:form is Thymeleaf’s enhanced version of the standard HTML <form> tag, built specifically to play nice with Spring Boot and Spring MVC. Here’s why it’s essential:
- Model Binding: It links your form directly to a model attribute (like a Java POJO) from your controller. For example, if your controller passes a
Userobject as@ModelAttribute("user"), usingth:form th:object="${user}"will map all your form fields to theUser’s properties automatically. - CSRF Protection: It adds a hidden CSRF token input to your form by default—critical for preventing cross-site request forgery attacks. Spring MVC automatically validates this token when the form is submitted, so you don’t have to manually add it.
- Error Handling: It works seamlessly with Spring’s validation. If your controller returns the form with validation errors, Thymeleaf can easily display those errors next to the relevant fields using
th:errors.
Difference between th:action and th:formaction
This is where a lot of new developers get confused—let’s break it down with examples:
th:action
- Applied to the
<form>tag itself (replacing the standard HTMLactionattribute). - Sets the default URL that the entire form will submit to, unless a submit button overrides it.
- Example:
<th:form th:action="@{/users/save}" th:object="${user}"> <input type="text" th:field="*{name}" /> <input type="email" th:field="*{email}" /> <button type="submit">Save User</button> </th>
Clicking the "Save User" button here will send the form data to /users/save.
th:formaction
- Applied to individual submit buttons (not the form tag).
- Overrides the form’s default
th:actionURL for that specific button. This is perfect when you have multiple actions in one form—like saving and deleting the same entity. - Example:
<th:form th:action="@{/users/save}" th:object="${user}"> <input type="text" th:field="*{name}" /> <input type="email" th:field="*{email}" /> <button type="submit" th:formaction="@{/users/save}">Save</button> <button type="submit" th:formaction="@{/users/delete/{id}(id=${user.id})}">Delete</button> </th>
Here, "Save" submits to /users/save, while "Delete" sends the form data to /users/delete/123 (assuming the user’s ID is 123).
Quick Troubleshooting Tip for Your Submission Issue
If your form stopped working after formatting code, double-check these common pitfalls:
- Did you accidentally remove the
th:objectattribute fromth:form? Without it, Thymeleaf can’t bind your form fields to the model, which will break submission. - Are you using Thymeleaf’s URL syntax
@{...}for your actions? Raw strings won’t resolve correctly in Spring Boot apps—always wrap your endpoints in@{}. - If you’re using
th:formactionon buttons, make sure the form still has a valid defaultth:action(or every button has its ownth:formaction). If there’s no target URL, the form won’t submit.
内容的提问来源于stack exchange,提问作者Sumit Ghosh

