html表单的put方法,form表单put、delete方式提交处理

form表单put、delete方式提交处理

form表单只支持get和post的方式提交,而我们使用restful风格必然要使用到@PUTmapping、@DELETEmapping等注解,那么在提交表单时的method=“put/delete”也要对应注解,下面来看看SpringMVC和SpringBoot的处理方式。

1、SpringMVC的处理

SpringMVC通过在web.xml中配置如下过滤器HiddenHttpMethodFilter处理;然后在页面上提交一个隐藏的input来实现;

1、配置HiddenHttpMethodFilter

HttpMethodFilterfilter-name> org.springframework.web.filter.HiddenHttpMethodFilterfilter-class> filter> HttpMethodFilterfilter-name> /*url-pattern> filter-mapping>

2、表单处理

form>

注意这里的name属性只能为_method,value为我们想提交的方式;

2、SpringBoot的处理

SpringBoot已经为我们自动配置了HttpMethodFilter,SpringBoot2.2.0以上版本需要我们手动开启配置;

1、开启配置

#开启hiddenmethod 过滤器

spring.mvc.hiddenmethod.filter.enabled=true

2、表单处理

同上;

3、看看源码

Ctrl+N搜索-WebMvcAutoConfiguration类

然后Ctrl+F搜索-hiddenmethod

看看SpringBoot如何自动配置的

源码如下:

@Bean

@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)

//意为当我们配置文件中没找到这个前缀(prefix)为spring.mvc.hiddenmethod.filter的名为enabled的属性就不开启这项配置,即默认false;

@ConditionalOnProperty(prefix = "spring.mvc.hiddenmethod.filter", name = "enabled", matchIfMissing = false)

public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() { return new OrderedHiddenHttpMethodFilter();

}

按住Ctrl并点击HiddenHttpMethodFilter.class,去看看过滤器的源码

public class HiddenHttpMethodFilter extends OncePerRequestFilter{ private static final List ALLOWED_METHODS;//允许的方法 public static final String DEFAULT_METHOD_PARAM = "_method"; private String methodParam = "_method";//这就是name="_method",也就是从页面拿到的参数 public HiddenHttpMethodFilter(){ } public void setMethodParam(String methodParam){ Assert.hasText(methodParam, "'methodParam' must not be empty"); this.methodParam = methodParam; } protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException{ HttpServletRequest requestToUse = request; //这里if判断request是不是post,这也是为什么form必须为post的原因 if ("POST".equals(request.getMethod()) && request.getAttribute("javax.servlet.error.exception") == null) { //利用request.getParameter取到hidden的input传来的value; String paramValue = request.getParameter(this.methodParam); //hasLength()判断paramValue不为空即将paramValue转为大写并赋值给method if (StringUtils.hasLength(paramValue)) { //这个method才是我们真正想要的请求方式 String method = paramValue.toUpperCase(Locale.ENGLISH); //如果被允许的method中包含有我们真正想要的请求方式 if (ALLOWED_METHODS.contains(method)) { //就使用我们希望的请求方式,把不是我们传的method过滤掉了 requestToUse = new HiddenHttpMethodFilter.HttpMethodRequestWrapper(request, method); } } } filterChain.doFilter((ServletRequest)requestToUse, response); } //这里定义了3种允许的方法DELETE、PUT、PATCH static { ALLOWED_METHODS = Collections.unmodifiableList(Arrays.asList(HttpMethod.PUT.name(), HttpMethod.DELETE.name(), HttpMethod.PATCH.name())); } private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper{ private final String method; public HttpMethodRequestWrapper(HttpServletRequest request, String method){ super(request); this.method = method; } public String getMethod(){ return this.method; } }

}

文章来源: segmentfault.com,作者:胖椿,版权归原作者所有,如需转载,请联系作者。

原文链接:segmentfault.com/a/1190000038389511

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HTML 中的 form 元素有以下几种方法: 1. GET 方法:使用 GET 方法提交表单时,表单数据会附加在 URL 后面,以查询字符串的形式发送到服务器。GET 方法适合用于获取数据的场合,比如搜索、过滤等。GET 方法不适合用于提交敏感数据,因为 URL 会被保存在浏览器历史记录中,容易被恶意利用。 ```html <form action="/search" method="get"> <input type="text" name="q"> <button type="submit">搜索</button> </form> ``` 2. POST 方法:使用 POST 方法提交表单时,表单数据会作为请求体发送到服务器。POST 方法适合用于提交、修改数据的场合,比如注册、登录等。POST 方法相对安全,因为请求体不会被保存在浏览器历史记录中。 ```html <form action="/login" method="post"> <label>用户名:<input type="text" name="username"></label> <label>密码:<input type="password" name="password"></label> <button type="submit">登录</button> </form> ``` 3. PUT 方法:使用 PUT 方法提交表单时,会将整个表单数据作为请求体发送到服务器,用于更新资源。但是,HTML 中的 form 元素不支持 PUT 方法,需要借助 JavaScript 或其他工具库实现。 4. DELETE 方法:使用 DELETE 方法提交表单时,会删除指定资源。但是,HTML 中的 form 元素同样不支持 DELETE 方法,需要借助 JavaScript 或其他工具库实现。 5. PATCH 方法:使用 PATCH 方法提交表单时,会对指定资源进行局部更新。HTML 中的 form 元素同样不支持 PATCH 方法,需要借助 JavaScript 或其他工具库实现。 除了以上几种方法,还有一些其他的 HTTP 方法,如 HEAD、OPTIONS、CONNECT、TRACE 等,但它们不常用于表单提交

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值