1.使用實體Bean,屬性名和請求參數的名稱要相同,適用於post ,get。
@RequestMapping("/index")
public String login(User user,HttpSession session,Model model){
......
}
2.直接將參數名放在方法中,適用於post ,get。
@RequestMapping("/index")
public String login(String name,String pwd,Model model){
......
}
3.通過HttpServletRequest接受請求參數,適用於post ,get。
@RequestMapping("/index")
public String login(HttpServletRequest request,Model model){
String name =request.getParameter("name");
String pwd =request.getParameter("password");
......
}
4.通過@PathVariable接受URL中的請求參數,適用於get。 RequestMapping中的值和參數按照名字自動對應
@RequestMapping("/index/{name}/{password}")
public String login(@PathVariable String name,@PathVariable String password,Model model){
......
}
5.通過@RequestParam接受請求參數,適用於post ,get。和方法2的不同是:當參數名不一致時,方法2不會報404錯誤,而本方法會報錯。
@RequestMapping("/index")
public String login(@RequestParam String name,@Request String password,Model model){
......
}
6.使用@ModelAttribute接受請求參數,適用於post ,get。 此時,就會將接受到的參數,存儲在ModelAttribute中,等同於model.addAttribute(“user”,user)
@RequestMapping("/index")
public String login(@ModelAttribute("user") User user){
......
}