Spring-web源码解析之Filter-HiddenHttpMethodFilter

基于4.1.7.RELEASE

就如同它的名字,该类负责解析隐藏的HttpMethod,用了这个Filter之后,你可以在页面上POST时指定_method参数,该Filter会根据参数指定的值将Request包装成为指定的HttpMethod的request。需要注意的有两点

1 必须是POST方式才进行处理

2 可以通过设置methodParam来更改参数名字,默认为_method。

主要代码如下

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
      throws ServletException, IOException {

   String paramValue = request.getParameter(this.methodParam);
   if ("POST".equals(request.getMethod()) && StringUtils.hasLength(paramValue)) {
      String method = paramValue.toUpperCase(Locale.ENGLISH);
      HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, method);
      filterChain.doFilter(wrapper, response);
   }
   else {
      filterChain.doFilter(request, response);
   }
}

根据注意事项1,如果是GET则直接进行下一个Filter处理。如果是POST,_method参数所传递进来的所有值都会被转换为大写。

附包装器代码

private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {

   private final String method;

   public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
      super(request);
      this.method = method;
   }

   @Override
   public String getMethod() {
      return this.method;
   }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值