首页 > 代码库 > Spring MVC ,无法请求以PUT ,DELETE等为 http方式的解决方法

Spring MVC ,无法请求以PUT ,DELETE等为 http方式的解决方法

@RequestMapping(value = "http://www.mamicode.com/test1.do",method = RequestMethod.DELETE)
public String testMapping1(HttpServletRequest request, HttpServletResponse response, ModelMap map){
    System.out.println("tj==========11==========test");
    String loginparams=request.getParameter("loginparams");
    System.out.println("tj=============loginparams================="+loginparams);
    map.put("tj","aaaaaa");

    return "test1";
}

  

method = RequestMethod.DELETE 时候,前端 跳转至 test1.do,type="DELETE",无法进入该方法
            $.ajax({
                  type: "DELETE",  //方式为DELETE,进不来
                  url: "/test1.do",
                  //data:"loginparams="+JSON.stringify({"name":"taojian","age":18}),
                  data:{"loginparams":[{"name":"taojian","age":18}]},
                  success: function(data){
                      alert(data)
                  }
              });

  

解决方法: web.xml 增加下面过滤器

<!--可以请求到 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>
<servlet-name>dispatcher</servlet-name>
</filter-mapping>

 

Spring MVC ,无法请求以PUT ,DELETE等为 http方式的解决方法