SpringMVC/Ajax 支持 PUT等请求

都是Restful 风格的错

    Rest风格的URL
    以CRUD为例
    新增:/order POST
    修改:/order/1 PUT         update?id=1
    获取:/order/1 GET         get?id=1  
    删除:/order/1 DELETE      delete?id=1

Form表单

配置HiddenHttpMethodFilter,表单才支持把POST请求转为 DELETEPUT

配置HttpPutFormContentFilterb则是支持put请求,我在测试ajax传递参数时,Controller获取不到参数,就是缺少这个过滤器。

<!-- 配置 HiddenHttpMethodFilter 可以把POST请求转为 DELETE 和 PUT请求 -->
  <filter>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

 <filter>
    <filter-name>httpPutFormcontentFilter</filter-name>
    <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>httpPutFormcontentFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

html

    测试4种请求
    <a href="springmvc/testRest/1">Test Rest GET</a><br>

    <form action="springmvc/testRest" method="post">
        <input type="submit"value="Test Rest POST"/>
    </form><br>

    <form action="/springmvc/testRest/2" method="post">  
        <input type="hidden" name="_method" value="delete"/>  
        <input type="submit" value="Test Rest DELETE"/>  
    </form> <br>
    <form action="/springmvc/testRest/3" method="post">
        <input type="hidden" name="_method" value="put"/>
        <input type="submit"value="Test Rest PUT"/>
    </form>

注意:Tomcat8.0 或者 web.xml 3.1 好像不支持put和 delete,我不确定,你找资料看一下

Controller

    ...
    @RequestMapping(value="/testRest/{id}",method=RequestMethod.GET)
    public String testRest(@PathVariable Integer id){
        System.out.println("testRest GET"+id);
        return SUCCESS;
    }
    @RequestMapping(value="/testRest",method=RequestMethod.POST)
    public String testRestPOST(){
        System.out.println("testRest POST");
        return SUCCESS;
    }

    @RequestMapping(value="/testRest/{id}",method=RequestMethod.DELETE)
    public String testRestDELETE(@PathVariable Integer id){
        System.out.println("testRest DELETE"+id);
        return SUCCESS;
    }
    @RequestMapping(value="/testRest/{id}",method=RequestMethod.PUT)
    public String tesPUTt(@PathVariable Integer id){
        System.out.println("testRest PUT"+id);
        return SUCCESS;
    }

Ajax 请求

web.xml同上,需要配置HiddenHttpMethodFilterHttpPutFormContentFilterb

jsp

     var form =  $('#updateform');
        //发送ajax请求
                $.ajax({
                    url:'你的请求',
                    async:false,//同步,会阻塞操作
                    type:'put',
                    timeout:1000,
                    data:form.serialize(),//这里的数据为表单数据
                    success:function (result) {
                        console.log(result);//输出返回结果
                        if(result['success']){ 
                         //请求成功
                            window.location.reload();
                        }else{
                           //请求失败
                        }
                    },error:function () {
                        //请求失败
                    }
                })

Controller

 @RequestMapping(value = "/映射的请求民", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map updatemsg( User user) {
        Map map = new HashMap();
        boolean result = true;
        int count = xxx逻辑;
        if (count == 0) {
            //更新失败
            result = false;
        }
        map.put("success", result);
        return map;
    }

完整的 Ajax 表单例子>>

http://blog.csdn.net/peng_hong_fu/article/details/70809525

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值