SpringMVC怎么获取前台传来的数据?
展开全部
1.通过HttpServletRequest:
@RequestMapping(value="/index1")
public String helloaction1(HttpServletRequest request){
System.out.println(request.getParameter("nnn")); //获得前台name为nnn的元素的值
return "index";
}
2.通过参数名获得:
@RequestMapping(value="/index1")
public String helloaction1(String nnn){ //这里名字要与前端元素名字一致才能获得
System.out.println(nnn);
return "index";
}
3.通过@RequestParam注解获得:
@RequestMapping(value="/index")
public String helloaction(@RequestParam(value="nnn",required=false)String nnn1, Model model){ //nnn要与前端一致,在此处可以理解为参数nnn1的别名
System.out.println(nnn1);
model.addAttribute("hello", "这是用action传过来的值:"+nnn1);
return "index";
}
4.SpringMvc还能通过将vo作为参数获得vo的各个属性:
@RequestMapping(value="/index2")
public String helloaction2(User user){
System.out.println(user.getAccount());
System.out.println(user.getPassword());
return "index";
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询