1.使用原始的方式,通过请求对象来获取参数
@RequestMapping("/login")
public void login(HttpServletRequest request){
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username);
System.out.println(password);
}
2.使用同名形参的方式(表单的参数名要和形参的相同,这种方式适用于参数数量较少的时候)
表单的参数名和形参的名称不同,就使用requestParam注解
@RequestMapping("/login")
public void login(@RequestParam("username") String username, String password){
System.out.println(username);
System.out.println(password);
}
3.将参数封装到对象中,使用对象来接收
@RequestMapping("/login")
public void login(User u){
System.out.println(u.getPassword());
System.out.println(u.getUsername());
}
public class User {
private String username;
private String password;
}
4.当前台是使用地址栏传递参数的时候.例如使用restful风格的时候
如localhost/login/5
@RequestMapping("/login/{id}")
public void login(@PathVariable("id") Long id ){
System.out.println(id);
}
5.针对于请求参数中有中文的问题,可以在web.xml中配置一个编码过滤器
但是只针对于post请求有效,如果是get请求,还需要使用原始的方式来配置,所以一般不建议在get请求中有中文参数