spring接收ajax參數的幾種方式

來源:互聯網
上載者:User

標籤:color   data   attr   str   stp   pos   response   完全   use   

參考網址:spring接收ajax參數的幾種方法@ModelAttribute 註解

使用@ModelAttribute這個方法可以直接將參數映射成pojo對象,我不加@ModelAttribute註解,直接接收pojo對象,同樣能夠接收到參數

前端ajax請求
        <script type="text/javascript">            $(function(){                $.ajax({                    type:"post",                    url:"http://localhost:8080/test",                    data:{                        "name":"張三",                        "phone":"10086",                        "password":"123456"                    },                    async:true,                    success:function(data){                        console.log(data);                    }                });            })        </script>
java接收參數
    @RequestMapping("/test")    @ResponseBody    public String testUser(@ModelAttribute("TUser") TUser user){        System.out.println(user.getName());        System.out.println(user.getPassword());        System.out.println(user.getPhone());        return "ok";    }
映射結果

@PathVariabl 註解

@PathVariable 是把請求的路徑上的指定的分段點映射成指定的參數名稱,@PathVariable可以指定多個,如果一個參數都不放在url路徑中,直接請求訪問。http://localhost:8080/test。同樣能夠接收到參數

前端ajax請求
        <script type="text/javascript">            $(function(){                $.ajax({                    type:"post",                    url:"http://localhost:8080/test/10086",                    data:{                        "name":"張三",                        "password":"123456"                    },                    async:true,                    success:function(data){                        console.log(data);                    }                });            })        </script>
java接收參數
    @RequestMapping("/test/{phone}")    @ResponseBody    public String testUser(String name,@PathVariable String phone,String password){        System.out.println(name);        System.out.println(phone);        System.out.println(password);        return "ok";    }
映射結果

直接用HttpServletRequest擷取前端代碼等同於案例一java接收參數
    @RequestMapping("/test")    @ResponseBody    public String testUser(HttpServletRequest request, HttpServletResponse response){        System.out.println(request.getParameter("name"));        System.out.println(request.getParameter("phone"));        System.out.println(request.getParameter("password"));        return "ok";    }
映射結果完全相同於案例一@RequestParam綁定請求參數前端請求代碼等同於案例一java接收參數
    @RequestMapping("/test")    @ResponseBody    public String testUser(@RequestParam("name") String a,@RequestParam("phone") String b, String password){        /**         * @RequestParam() 裡邊的值必須要和前端傳遞過來的參數名字相同         */        System.out.println(a);        System.out.println(b);        System.out.println(password);        return "ok";    }
映射結果完全相同總結:

1、如果前端傳遞過來的參數與後台介面定義的參數名相同,可以不需要任何註解,如果所有參數都在一個pojo對象中,那麼可以直接映射成pojo對象,也可以使用  HttpServletRequest 接收參數

2、如果想使用 resfull 風格的請求方式的話,可以使用 @PathVariable 進行註解

3、如果前端傳遞的參數與後台介面定義的參數名稱不相同,那麼就使用 RequestParam 註解

 

spring接收ajax參數的幾種方式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.