springboot的參數封裝, 和springmvc相識spring
一、直接把表單的參數寫在Controller相應的方法的形參中,適用於get方式提交,不適用於post方式提交。數組
/** * 1.直接把表單的參數寫在Controller相應的方法的形參中 * @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"; }
二、經過HttpServletRequest接收,post方式和get方式均可以。springboot
/** * 二、經過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"; }
三、經過一個bean來接收,post方式和get方式均可以。mvc
(1)創建一個和表單中參數對應的beanapp
package demo.model; public class UserModel { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
(2)用這個bean來封裝接收的參數post
/** * 三、經過一個bean來接收 * @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"; }
四、經過@PathVariable獲取路徑中的參數ui
/** * 四、經過@PathVariable獲取路徑中的參數 * @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"; }
五、使用@ModelAttribute註解獲取POST請求的FORM表單數據this
<form action ="<%=request.getContextPath()%>/demo/addUser5" method="post"> 用戶名: <input type="text" name="username"/><br/> 密 碼: <input type="password" name="password"/><br/> <input type="submit" value="提交"/> <input type="reset" value="重置"/> </form>
controllerspa
/** * 五、使用@ModelAttribute註解獲取POST請求的FORM表單數據 * @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"; }
六、用註解@RequestParam綁定請求參數到方法入參.net
當請求參數username不存在時會有異常發生,能夠經過設置屬性required=false解決,例如: @RequestParam(value="username", required=false)
/** * 六、用註解@RequestParam綁定請求參數到方法入參 * @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"; }
複雜參數的封裝
複雜參數是指 數組, 集合等對象, 其中bean屬性對應參數的封裝:
未完待續....
原文連接: http://blog.csdn.net/yalishadaa/article/details/68937141