1、使用HttpServletRequest获取
@RequestMapping("/login.do")
public String login(HttpServletRequest request){
String name = request.getParameter("name")
String pass = request.getParameter("pass")
}
2、自动注入Bean属性
<form action="login.do">
用户名:<input name="name"/>
密码:<input name="pass"/>
<input type="submit" value="登陆">
</form>
//封装的User类
public class User{
private String name;
private String pass;
}
form表单提交的参数自动注入到User里面去,可以使用User里的get方法获取值@RequestMapping("/login.do")
public String login(User user)
{
syso(user.getName());
syso(user.getPass());
}
3、Spring会自动将表单参数注入到方法参数,和表单的name属性保持一致,和Struts2一样,可使用注解@RequestParam设置默认值@RequestMapping("/login.do")
public String login( @RequestParam("pass",defaultValue="0")String password) // 表单属性是pass,默认为0,用变量password接收
{
syso(password)
}
4、通过@PathVariabl获取路径中的参数
@RequestMapping(value="user/{id}/{name}",method=RequestMethod.GET)
public String printMessage1(@PathVariable String id,@PathVariable String name, ModelMap model) {
System.out.println(id);
System.out.println(name);
model.addAttribute("message", "111111");
return "users";
}
5、@ModelAttribute获取POST请求的FORM表单数据
<form method="post" action="hao.do">
a: <input id="a" type="text" name="a"/>
b: <input id="b" type="text" name="b"/>
<input type="submit" value="Submit" />
</form>
public class Pojo{
private String a;
private int b;
}
@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pojo") Pojo pojo) {
return "helloWorld";
}