后台将数据传回前台的三种绑定的方式(Model,Map.ModelAndView)

时间:2022-11-20 08:24:58
//方式1:通过model 将数据绑定
@RequestMapping(value = "findByIdModel", method = RequestMethod.GET)
public String findByIdModel(@RequestParam("empno") int id, Model model) { Emp emp = empService.findById(id);//从业务层传回的数据 model.addAttribute("emp", emp);//将数据绑定 return "findbyid";//返回的页面
}
//方式2:通过Map
@RequestMapping(value = "findByIdMap", method = RequestMethod.GET)
public String findByIdMap(@RequestParam("empno") int id, Map<String,Object> map) { Emp emp = empService.findById(id); map.put("emp", emp); return "findbyid";
} //方式3:通过ModelAndView
@RequestMapping(value = "findByIdMAV", method = RequestMethod.GET)
public ModelAndView findByIdMAV(@RequestParam("empno") int id) { Emp emp = empService.findById(id); // ModelAndView mav = new ModelAndView("findbyid");
ModelAndView mav = new ModelAndView(); mav.setViewName("findbyid");//设置返回页面 mav.addObject("emp", emp);//设置action传递的值 System.out.println("use ModelAndView:"+emp); return mav;
}