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接收前台传递过来的值的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
热门推荐
1 天仓节祝福语简短
10 中秋骚气祝福语简短情人
11 厨房祝福语简短100句
12 男孩中考蛋糕祝福语简短
13 开张祝福语简短小故事
14 圣诞祝福语简短贺卡英文
15 过年拜年祝福语大全简短
16 正月十五简短谜语祝福语
17 红包祝福语简短10字
18 2026腊八简短祝福语