Sumit Ghosh
Sumit Ghosh

Reputation: 514

Working Difference between th:action and th:formaction in Thymeleaf

I am new to Thymeleaf.I am having a Spring-Boot app which used Thymeleaf component in view pages.

Now while using eclipse code assist I came across few thymeleaf tag/attributes namely th:form,th:formaction.

Once I change my above code to following format :

Its stopped working.My webpage is not getting submitted to server. So,I wanted to understand the following things:

What is the use of th:form tag? What is the difference between th:action and th:formaction tag?

Upvotes: 0

Views: 16901

Answers (1)

Stephan
Stephan

Reputation: 940

The ´th:action´ and ´th:formaction´ tags will create the html tags action and formaction. This question is not really related to Thymeleaf but the html tags itself.

The action tag can be placed onto a form to specify the url to pass the form to. In your first example submitting the form will send a POST request to /saveStudent.

The second example is not valid HTML that's why the form will not submit. formaction can be used to override the action attribute of the form within the form. It can be used on input tags:

<form th:action="@{/saveStudent}" th:object="${user}" method="post">
  <table>
    <tr> 
      <td>Enter Your name</td>
      <td><input type="text"  th:field="*{userName}"/></td>
      <td><input type="submit" value="Submit"/></td>
      <td><input th:formaction="@{/saveSomewhereElse}" type="submit" value="Submit to other url"/></td>
    </tr>
  </table>
</form>

In this example the default action is to submit to /saveStudent but if someone clicks the second submit button the form will submit to /saveSomewhereElse.

In general you probably will need just the action tag in 99% of the cases.

Upvotes: 8

Related Questions