SpringMVC处理put、delete请求

为了符合restful风格,最近在做项目时,除了GET和POST请求,还决定使用PUT和DELETE请求,但是在使用springMVC进行这个两个类型的请求时,如下:

 $.ajax({
        url:‘***’,
        type:‘put’,
        data:{}
      })
发现controller接收的自定义实体类形参的各个属性都是null,原因是浏览器本身只支持get和post方法,因此需要使用_method这个隐藏字段来告知spring这是一个put请求。所以type是不能变动的,否则浏览器不支持。 为此,spring3.0添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET、POST、PUT与DELETE请求,该过滤器是HiddenHttpMethodFilter。

 /** Default method parameter: <code>_method</code> */
        public static final String DEFAULT_METHOD_PARAM = "_method";

        private String methodParam = DEFAULT_METHOD_PARAM;


        /**
         * Set the parameter name to look for HTTP methods.
         * @see #DEFAULT_METHOD_PARAM
         */
        public void setMethodParam(String methodParam) {
            Assert.hasText(methodParam, "'methodParam' must not be empty");
            this.methodParam = methodParam;
        }

        @Override
        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);
    }
}
查看这个filter的源码你会发现这个过滤器的原理其实就是将http请求中的_method属性值在doFilterInternal方法中转化为标准的http方法。
需要注意的是,doFilterInternal方法中的if条件判断http请求是否POST类型并且请求参数中是否含有_method字段,也就是说方法只对method为post的请求进行过滤,所哟请求必须如下设置:

$.ajax({
        url:‘***’,
        type:‘post’,
        data:{
                 _method:put,
              }
      }) 
同时HiddenHttpMethodFilter必须作用于dispatch前,所以需要在web.xml中配置一个filter

 <filter>  
    <filter-name>HiddenHttpMethodFilter</filter-name>  
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>  
</filter>  

<filter-mapping>  
    <filter-name>HiddenHttpMethodFilter</filter-name>  
    <servlet-name>ROOT</servlet-name>  
</filter-mapping>
这样,springmvc就能处理put和delete请求了。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值