springboot获取URL请求参数的几种方法
原文地址:http://www.cnblogs.com/xiaoxi/p/5695783.html
?
1、直接把表單的參數(shù)寫(xiě)在Controller相應(yīng)的方法的形參中,適用于get方式提交,不適用于post方式提交。
/*** 1.直接把表單的參數(shù)寫(xiě)在Controller相應(yīng)的方法的形參中* @param username* @param password* @return*/@RequestMapping("/addUser1")public String addUser1(String username,String password) {System.out.println("username is:"+username);System.out.println("password is:"+password);return "demo/index";}url形式:http://localhost/SSMDemo/demo/addUser1?username=lixiaoxi&password=111111?提交的參數(shù)需要和Controller方法中的入?yún)⒚Q(chēng)一致。
2、通過(guò)HttpServletRequest接收,post方式和get方式都可以。
/*** 2、通過(guò)HttpServletRequest接收* @param request* @return*/@RequestMapping("/addUser2")public String addUser2(HttpServletRequest request) {String username=request.getParameter("username");String password=request.getParameter("password");System.out.println("username is:"+username);System.out.println("password is:"+password);return "demo/index";}3、通過(guò)一個(gè)bean來(lái)接收,post方式和get方式都可以。
(1)建立一個(gè)和表單中參數(shù)對(duì)應(yīng)的bean
(2)用這個(gè)bean來(lái)封裝接收的參數(shù)
/*** 3、通過(guò)一個(gè)bean來(lái)接收* @param user* @return*/@RequestMapping("/addUser3")public String addUser3(UserModel user) {System.out.println("username is:"+user.getUsername());System.out.println("password is:"+user.getPassword());return "demo/index";}4、通過(guò)@PathVariable獲取路徑中的參數(shù)
/*** 4、通過(guò)@PathVariable獲取路徑中的參數(shù)* @param username* @param password* @return*/@RequestMapping(value="/addUser4/{username}/{password}",method=RequestMethod.GET)public String addUser4(@PathVariable String username,@PathVariable String password) {System.out.println("username is:"+username);System.out.println("password is:"+password);return "demo/index";}
例如,訪問(wèn)http://localhost/SSMDemo/demo/addUser4/lixiaoxi/111111?路徑時(shí),則自動(dòng)將URL中模板變量{username}和{password}綁定到通過(guò)@PathVariable注解的同名參數(shù)上,即入?yún)⒑髐sername=lixiaoxi、password=111111。
5、使用@ModelAttribute注解獲取POST請(qǐng)求的FORM表單數(shù)據(jù)
Jsp表單如下:
Java Controller如下:
/*** 5、使用@ModelAttribute注解獲取POST請(qǐng)求的FORM表單數(shù)據(jù)* @param user* @return*/@RequestMapping(value="/addUser5",method=RequestMethod.POST)public String addUser5(@ModelAttribute("user") UserModel user) {System.out.println("username is:"+user.getUsername());System.out.println("password is:"+user.getPassword());return "demo/index";}6、用注解@RequestParam綁定請(qǐng)求參數(shù)到方法入?yún)?/p>
當(dāng)請(qǐng)求參數(shù)username不存在時(shí)會(huì)有異常發(fā)生,可以通過(guò)設(shè)置屬性required=false解決,例如: @RequestParam(value="username", required=false)
/*** 6、用注解@RequestParam綁定請(qǐng)求參數(shù)到方法入?yún)? @param username* @param password* @return*/@RequestMapping(value="/addUser6",method=RequestMethod.GET)public String addUser6(@RequestParam("username") String username,@RequestParam("password") String password) {System.out.println("username is:"+username);System.out.println("password is:"+password);return "demo/index";}?
總結(jié)
以上是生活随笔為你收集整理的springboot获取URL请求参数的几种方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 软件包管理 2 -----基本知识 rp
- 下一篇: springboot2自定义HttpTr