1、通过HttpServletRequest接收,post方式和get方式都可以。
2、用注解@RequestParam绑定请求参数到方法入参
3、通过一个bean来接收,post方式和get方式都可以。
(1)建立一个和表单中参数对应的bean
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来封装接收的参数
@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、通过@PathVariable获取路径中的参数
@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";
}
例如,访问http://localhost/demo/addUser4/lixiaoxi/111111路径时,则自动将URL中模板变量{username}和{password}绑定到通过@PathVariable注解的同名参数上,即入参后username=mayun、password=111111。
5、直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交
url形式:http://localhost/SSMDemo/demo/addUser1?username=mayun&password=111 提交的参数需要和Controller方法中的入参名称一致。