SpringMVC接收前台传递过来的值的实例
之前控制器方法获得前台传来的值有三种方式:
1.通过HttpServletRequest:
@RequestMapping(value="/index1")
publicStringhelloaction1(HttpServletRequestrequest){
System.out.println(request.getParameter("nnn"));//获得前台name为nnn的元素的值
return"index";
}
2.通过参数名获得:
@RequestMapping(value="/index1")
publicStringhelloaction1(Stringnnn){//这里名字要与前端元素名字一致才能获得
System.out.println(nnn);
return"index";
}
3.通过@RequestParam注解获得:
@RequestMapping(value="/index")
publicStringhelloaction(@RequestParam(value="nnn",required=false)Stringnnn1,Modelmodel){//nnn要与前端一致,在此处可以理解为参数nnn1的别名
System.out.println(nnn1);
model.addAttribute("hello","这是用action传过来的值:"+nnn1);
return"index";
}
SpringMvc还能通过将vo作为参数获得vo的各个属性:
@RequestMapping(value="/index2")
publicStringhelloaction2(Useruser){
System.out.println(user.getAccount());
System.out.println(user.getPassword());
return"index";
}
使用对象进行获取数据的时候要注意,前端页面的元素name属性要与vo的各个属性名字一致
以上这篇SpringMVC接收前台传递过来的值的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
热门推荐
10 新郎表白成语祝福语简短
11 接亲新郎进门祝福语简短
12 结婚转账祝福语高级简短
13 年前幸福祝福语大全简短
14 鲜花卡片简短开业祝福语
15 老师及家人祝福语简短
16 最美母女祝福语简短英文
17 男孩上学祝福语简短英语
18 最美婆婆生日祝福语简短