Using hidden type when PUT with Thymeleaf

When PUT with Thymeleaf, PUT is not simply specified in the action of the form method.

Background of investigation

I implemented it so that it can be PUT, but when I was looking at the Request Method in the developer console of chrome, it turned out to be POST? Because it became. ..

image.png

Looking at From Data, it is put. image.png

See how the Thymeleaf source is deployed

After specifying post for the action of the form tag, you can see from the source that put is realized by using the hidden type. ・ What is hidden type? https://developer.mozilla.org/ja/docs/Web/HTML/Element/Input/hidden It is used when you want to send a value without displaying it on the form.

--Thymeleaf code

sample.html


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
  <head>
    <meta http-equiv='Content-type' content='text/html; charset=utf-8' />
    <title>test</title>
  </head>
  <body>
<!--Pay attention here-->
    <form th:action="@{/user}" th:method="put">
      <input class="btn btn-default btn-xs" type="submit" value="update" />
    </form>
<!--Pay attention here-->
  </body>
</html>

--Source code displayed on the screen (after thymeleaf expansion)

sample.html


<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv='Content-type' content='text/html; charset=utf-8' />
    <title>test</title>
  </head>
  <body>
<!--Pay attention here-->
    <form action="/user" method="post">
      <input type="hidden" name="_method" value="put"/>
      <input class="btn btn-default btn-xs" type="submit" value="update" />
    </form>
<!--Pay attention here-->
  </body>
</html>

Reason

The form tag only supports the get and post methods. Therefore, it seems that the method is to specify method =" post " on the form tag and specify value =" put " in the hidden type. As a result, Request Method is POST, but it is sent by PUT because it is sent by the method specified by _method. When I looked it up, it seems that other frameworks such as Rails also realize put in this way.

Recommended Posts

Using hidden type when PUT with Thymeleaf
Precautions when using checkboxes in Thymeleaf
Be careful when using rails_semantic_logger with unicorn
Specify ClassPath when using jupyter + Java with WSL
Make Volume faster when using Docker with vscode.
Thymeleaf: Output with a delimiter when outputting with th: each
Whether to enable SSL when using JDBC with MySQL.
Generate JavaScript with Thymeleaf
How to set environment variables when using Payjp with Rails
Be careful of initialization timing when using MessageEncryptor with Rails 5.2 / 6.0
When using ExpandableListView with fragment, exception occurs when inheritance is ListFragment
Precautions when using Mockito.anyString as an argument when Mocking with Mockito
Things to keep in mind when using Sidekiq with Rails