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

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

SpringMVC注解@RequestParam全面解析

2019-11-11 00:48:47
字體:
供稿:網(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ù)值會報錯,因為不傳值,會賦值為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ā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 加查县| 福州市| 临沂市| 延庆县| 无极县| 广饶县| 益阳市| 康平县| 大洼县| 曲靖市| 东光县| 青海省| 文登市| 武威市| 友谊县| 布拖县| 辽阳市| 枞阳县| 蒲江县| 库尔勒市| 汕尾市| 石阡县| 安宁市| 田林县| 珠海市| 青田县| 梅河口市| 拜泉县| 青海省| 许昌县| 肥乡县| 哈巴河县| 平南县| 营口市| 山东省| 宣威市| 增城市| 灌云县| 通河县| 宜兰市| 新化县|