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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值