SpringMvc如何获得前台传来的值
2018-06-22 · 国内最优秀java资源共享平台
通过HttpServletRequest
public String toIndex(HttpServletRequest request){
//获得前台name为username的元素的值
System.out.println(request.getParameter("username"));
return "index";}
通过参数名获得
@RequestMapping(value="/index")
public String toIndex(String username){ //这里名字要与前端元素名字一致才能获得
System.out.println(username);
return "index";
}通过@RequestParam注解获得
@RequestMapping(value="/index")
public String toIndex(@RequestParam(value="username",required=false)String username111){ //username要与前端一致,在此处可以理解为参数username111的别名
System.out.println(username111);
return "index";
}SpringMvc还能通过将vo作为参数获得vo的各个属性
@RequestMapping(value="/index")
public String toIndex(User user){
System.out.println(user.getUserName());
System.out.println(user.getPassword());
return "index";