之前控制器方法获得前台传来的值有三种方式:
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的各个属性:例如:user有account,password属性(**使用对象进行获取数据的时候要注意,前端页面的元素name属性要与vo的各个属性名字一致 )
那么前台直接传&account=zhangsan&password=10,不能传user.account=zhangsan&user.password=10
@RequestMapping(value="/index2")
public String helloaction2(User user){
System.out.println(user.getAccount());
System.out.println(user.getPassword());
return "index";
}
5.使用自定义对象获取前台数据,例如:后台有一个object对象,该对象里面有id,然后传入list集合,集合中是自定义的对象,每个对象也有id属性,例如下面这种:
后台如果要接收object对象和集合对象,那么此时不能传入“object.”,只能传入object的各个属性,前面不能加object,可以这么做:
public class RprjInfoVo {
private RprjInfoObject object;
private List<RprjElementObject> rprjElenmentList;
public void setObject(RprjInfoObject object) {
this.object = object;
}
public void setRprjElenmentList(List<RprjElementObject> rprjElenmentList) {
this.rprjElenmentList = rprjElenmentList;
}
public RprjInfoObject getObject() {
return object;
}
public List<RprjElementObject> getRprjElenmentList() {
return rprjElenmentList;
}
}
在controller中可以这么接收:
@Permission
@RequestMapping(value = "...")
@ResponseBody
public ResponseBaseBean addRprjInfo(ModelAndView model, RprjInfoVo rprjInfoVo) {
RprjInfoObject object = rprjInfoVo.getObject();
List<RprjElementObject> rprjElenmentList = rprjInfoVo.getRprjElenmentList();
}