You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Thymeleaf中th:action与th:formaction的区别及表单提交问题咨询

Thymeleaf's 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 User object as @ModelAttribute("user"), using th:form th:object="${user}" will map all your form fields to the User’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 HTML action attribute).
  • 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:action URL 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:object attribute from th: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:formaction on buttons, make sure the form still has a valid default th:action (or every button has its own th:formaction). If there’s no target URL, the form won’t submit.

内容的提问来源于stack exchange,提问作者Sumit Ghosh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:56:57