SpringMVC——如何获取请求参数

参考 http://www.cnblogs.com/bigdataZJ/p/springmvc2.html (文章讲了几个注解的使用,但不够深入。)

参考 http://www.cnblogs.com/xiaoxi/p/5695783.html  (这篇文章介绍的详细,有例子)

SpringMVC获取参数的方式可以分为以下几种:

1、通过@PathVariable 注解,获取@RequestMapping中占位符所代表的参数变量。如

@RequestMapping(value="/addUser4/{username}/{password}",method=RequestMethod.GET)
public String addUser4(@PathVariable String username,@PathVariable String password) {}

2、通过@RequestParam获取?号后的参数,一般用于get请求,如:

public String addUser6(@RequestParam("username") String username,@RequestParam("password") String password)

3、通过public String addUser2(HttpServletRequest request) 中的request对象获取,struts2中常用这种方式来获取。如:request.getParameter("username")

4、通过封装一个bean来接收。如public String addUser3(UserModel user) ,适用于get和post请求。

5、用方法形参作为接收参数名,一般用于get请求。如:

public String addUser1(String username,String password) {}
原文地址:https://www.cnblogs.com/aligege/p/6896040.html