<i id="nph9l"><b id="nph9l"><del id="nph9l"></del></b></i>

            專注Java教育13年 全國咨詢/投訴熱線:400-8080-105
            動力節點LOGO圖
            始于2009,一家只教授Java的培訓機構
            首頁 hot資訊 幾種SpringMVC接收參數的方式

            幾種SpringMVC接收參數的方式

            更新時間:2022-07-25 09:44:49 來源:動力節點 瀏覽318次

            直接根據屬性名和類型接收參數

            URL形式:

            ? http://localhost:8080/test/one 表單提交數據,其中屬性名與接收參數的名字一致;

            ? http://localhost:8080/test/one?name=aaa 數據顯示傳送

            注意:delete請求用表單提交的數據后臺無法獲取,得將參數顯示傳送;

            controller端的代碼

                @RequestMapping("/one")
                public String testOne(String name){
                    System.out.println(name);
                    return "success";
                }

            說明:直接將屬性名和類型定義在方法的參數中,前提是參數名字必須得跟請求發送的數據的屬性的名字一致,而且這種方式讓請求的參數可為空,請求中沒有參數就為null;

            通過bean來接收數據

            URL形式:

            http://localhost:8080/test/two 表單提交數據,其中屬性名與接收的bean內的屬性名一致;

            http://localhost:8080/test/two?username=aa&password=bb 數據顯示傳送

            構建一個Userbean

            public class Test { 
                private String username;
                private String password;
            }

            get,set,tostring沒貼上來

            后端請求處理代碼

                @RequestMapping("/two")
                public String testTwo(User user){
                    System.out.println(user.toString());
                    return "success";
                }

            說明:User類中一定要有set,get方法,springmvc會自動將與User類中屬性名一致的數據注入User中,沒有的就不注入;

            通過HttpServletRequest來獲取數據

            URL形式:

            ? http://localhost:8080/test/three 采用表單提交數據

            ? http://localhost:8080/test/three?username=aa 數據顯示傳送

            后端請求處理代碼:

                @RequestMapping("/three")
                public String testThree(HttpServletRequest request){
                    String username = request.getParameter("username");
                    System.out.println(username);
                    return "success";
                }

            說明:后端采用servlet的方式來獲取數據,但是都用上框架了,應該很少用這種方式來獲取數據了吧;

            通過@PathVariable獲取路徑參數

            URL形式

            http://localhost:8080/test/four/aaa/bbb

            后端請求處理代碼:

            @RequestMapping("/four/{username}/{password}")
                public String testFour(
                                       @PathVariable("username")String username,
                                       @PathVariable("password")String password
                ){
                    System.out.println(username);
                    System.out.println(password);
                    return "success";
                }

            說明:@PathVariable注解會將請求路徑中對應的數據注入到參數中

            注意:@PathVariable注解的數據默認不能為空,就是請求路徑中必須帶有參數,不能為空,如果請求數據為空,則需要在@PathVariable中將required屬性改為false;

            通過@RequestParam來獲取參數

            URL形式

            http://localhost:8080/test/five 表單提交數據,未顯示傳送

            http://localhost:8080/test/two?username=aa&password=bb 數據顯示傳送

            后端處理代碼

             @RequestMapping("/five")
                public String testFive(@RequestParam(value = "username")String username,
                                       @RequestParam("password")String password
                ){
                    System.out.println(username);
                    System.out.println(password);
                    return "success";
                }

            說明: @RequestParam會將請求中相對應的數據注入到參數中。

            注意: @RequestParam注解的參數也是不能為空的,如果需要為空,則需要將required屬性改為false,還有就是 @RequestParam注解中的defaultValue 屬性可以為參數設置默認值。

            提交申請后,顧問老師會電話與您溝通安排學習

            免費課程推薦 >>
            技術文檔推薦 >>
            返回頂部
            欧美系列A片

                <i id="nph9l"><b id="nph9l"><del id="nph9l"></del></b></i>