黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 幾種SpringMVC接收參數的方式

幾種SpringMVC接收參數的方式

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

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

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 屬性可以為參數設置默認值。

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

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 伊人久久大香线蕉综合7 | 操操干 | 国产黑丝袜在线 | wwwxxx欧美| 天天干天天添 | 午夜视频精品 | 欧美一级高清片在线 | 欧美精品导航 | 波多野结衣 一区二区 | 妞干网在线播放 | 日本不卡一区二区三区在线观看 | 国产精品视频第一区二区三区 | 午夜激情在线 | 狠狠色狠狠色综合日日32 | 中国一级做a爱片免费 | 亚洲va国产va欧美va综合 | 老湿影院免费体验区 | 亚洲大片免费观看 | 国产一卡二卡≡卡四卡无人 | 永久免费在线播放 | 中文字幕aⅴ在线视频 | 日日摸夜夜摸狠狠摸日日碰夜夜做 | 日本欧美视频在线观看 | 久操导航 | 亚欧有色在线观看免费版高清 | 午夜精品视频在线观看 | 一级aaaaaa毛片免费 | 国产一区视频在线免费观看 | 一级黄a | 国产精品久久久久久久久久一区 | 手机福利视频一区二区 | 高h浪诱受肉耽文 | 国产成人深夜福利在线观看 | 日日干狠狠操 | 宠文肉细致高h一对一 | 人人揉人人添人人捏人人看 | 日本精品一二三区 | 国产在线欧美精品中文一区 | 国产在播放一区 | 国产精品成人观看视频免费 | 欧美高清videosex极品 |