国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

SpringMVC注解@RequestParam全面解析

2019-11-11 00:45:33
字體:
供稿:網(wǎng)友
在SPRingMVC后臺控制層獲取參數(shù)的方式主要有兩種,一種是request.getParameter(“name”),另外一種是用注解@RequestParam直接獲取。這里主要講這個注解一、基本使用,獲取提交的參數(shù)后端代碼:java代碼  收藏代碼@RequestMapping(“testRequestParam”)       public String filesUpload(@RequestParam String inputStr, HttpServletRequest request) {        System.out.println(inputStr);            int inputInt = Integer.valueOf(request.getParameter(“inputInt”));      System.out.println(inputInt);            // ……省略      return “index”;     }     
   @RequestMapping("testRequestParam")      public String filesUpload(@RequestParam String inputStr, HttpServletRequest request) {          System.out.println(inputStr);        int inputInt = Integer.valueOf(request.getParameter("inputInt"));        System.out.println(inputInt);        // ......省略        return "index";    }   前端代碼:Html代碼  收藏代碼<form action=“/gadget/testRequestParam” method=“post”>         參數(shù)inputStr:<input type=“text” name=“inputStr”>         參數(shù)intputInt:<input type=“text” name=“inputInt”>    </form>  
<form action="/gadget/testRequestParam" method="post">       參數(shù)inputStr:<input type="text" name="inputStr">       參數(shù)intputInt:<input type="text" name="inputInt">  </form>前端界面:執(zhí)行結(jié)果:test1123可以看到spring會自動根據(jù)參數(shù)名字封裝進(jìn)入,我們可以直接拿這個參數(shù)名來用二、各種異常情況處理1、可以對傳入?yún)?shù)指定參數(shù)名Java代碼  收藏代碼@RequestParam String inputStr  // 下面的對傳入?yún)?shù)指定為aa,如果前端不傳aa參數(shù)名,會報錯  @RequestParam(value=“aa”) String inputStr  
@RequestParam String inputStr// 下面的對傳入?yún)?shù)指定為aa,如果前端不傳aa參數(shù)名,會報錯@RequestParam(value="aa") String inputStr錯誤信息:HTTP Status 400 - Required String parameter ‘a(chǎn)a’ is not present2、可以通過required=false或者true來要求@RequestParam配置的前端參數(shù)是否一定要傳Java代碼  收藏代碼// required=false表示不傳的話,會給參數(shù)賦值為null,required=true就是必須要有  @RequestMapping(“testRequestParam”)        public String filesUpload(@RequestParam(value=“aa”, required=true) String inputStr, HttpServletRequest request)  
// required=false表示不傳的話,會給參數(shù)賦值為null,required=true就是必須要有@RequestMapping("testRequestParam")      public String filesUpload(@RequestParam(value="aa", required=true) String inputStr, HttpServletRequest request)3、如果用@RequestMapping注解的參數(shù)是int基本類型,但是required=false,這時如果不傳參數(shù)值會報錯,因?yàn)椴粋髦担瑫x值為null給int,這個不可以Java代碼  收藏代碼@RequestMapping(“testRequestParam”)       public String filesUpload(@RequestParam(value=“aa”, required=true) String inputStr,           @RequestParam(value=“inputInt”, required=false) int inputInt          ,HttpServletRequest request) {              // ……省略      return “index”;     }  
    @RequestMapping("testRequestParam")      public String filesUpload(@RequestParam(value="aa", required=true) String inputStr,             @RequestParam(value="inputInt", required=false) int inputInt            ,HttpServletRequest request) {          // ......省略        return "index";    }解決方法:    “Consider declaring it as object wrapper for the corresponding primitive type.”建議使用包裝類型代替基本類型,如使用“Integer”代替“int”  
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 垣曲县| 塘沽区| 金山区| 江西省| 临澧县| 彭泽县| 邢台县| 拜城县| 濮阳市| 公主岭市| 新竹市| 文安县| 余干县| 青河县| 洪江市| 峨山| 梨树县| 宿州市| 会同县| 吉水县| 巨野县| 卢氏县| 鹤峰县| 醴陵市| 灯塔市| 溧水县| 板桥市| 涡阳县| 西和县| 潜江市| 安西县| 会同县| 合阳县| 浮梁县| 苍梧县| 广河县| 巨野县| 固安县| 东宁县| 利辛县| 金坛市|