Home > Java > javaTutorial > How to use PUT and Delete requests in SpringBoot

How to use PUT and Delete requests in SpringBoot

WBOY
Release: 2023-05-13 12:16:06
forward
1920 people have browsed it

PUT and Delete requests are used

In the Form form, only get and post methods are supported. In order to implement the put method

, we can implement it through the following three steps

1) Configure HiddenHttpMethodFilter in SpringMVC

2) Create a post form on the page

3) Create an input item, name="_method", the value is the specified request method

In the HiddenHttpMethodFilter class

How to use PUT and Delete requests in SpringBoot

get the value of "_method" and get the new request method.

How to use PUT and Delete requests in SpringBoot

<input type="hidden" name="_method" value="put" th:if="${employee!=null}"/>
Copy after login

The th tag is the thymeleaf template, which means it will only take effect when employee is not empty, and the put in value is not case-sensitive.

At that time, in the new version of SpringBoot, this put request did not work. The reason is that springboot automatically configures, which helps us omit the first step of configuration. The above code method is to achieve automatic configuration, but because the annotation @ConditionalOnProperty limits automatic configuration, the default false does not enable configuration, so the put submission of the page cannot be used.

Solution

Configure in the properties configuration file to enable automatic configuration: spring.mvc.hiddenmethod.filter.enabled=true.

In addition, DELETE requests can also be set in the same way.

<form th:action="@{/emp/}+${emp.id}" method="post">
	<input type="hidden" name="_method" value="delete"/>
	<button type="submit" class="btn btn-sm btn-danger" > 删除</button>
</form>
Copy after login

How to support put/delete requests

Anyone who has studied mvc knows that if you want to support these two special requests, you must first configure the following filters in web.xml:

<!--增加一个HiddenHttpMethodFilter过滤器:目的是给普通浏览器 增加put|delete请求方式-->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
Copy after login

SpringBoot is not so troublesome, because it has helped us incorporate HiddenHttpMethodFilter into the IOC container by default, so its use is extremely simple:

1. In application.properties Configuration

#开启支持put delete请求的过滤器
spring.mvc.hiddenmethod.filter.enabled=true
Copy after login

2. When used, it is still the same as springmvc

You only need to add the following hidden fields to the form form of the post request method:

     <!--http请求方式-->
     <form action="..."  method="post">
         <input type="hidden" name="_method" value="put" />
         <!--value值改成delete 请求方式就为delete了-->
     </form>
Copy after login

Pay attention to the hidden fields above The name must be "_method". If you want to modify it, you need to add the following bean to the IOC:

@Bean
public HiddenHttpMethodFilter hiddenHttpMethodFilter(){
    HiddenHttpMethodFilter methodFilter = new HiddenHttpMethodFilter();
    methodFilter.setMethodParam("_m");//将隐藏域 _method --> _m
    return methodFilter;
}
Copy after login

The above is the detailed content of How to use PUT and Delete requests in SpringBoot. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template