域对象共享数据 使用ServletAPI向request域对象共享数据 1 2 3 4 5 @RequestMapping("/testServletAPI") public String testServletAPI(HttpServletRequest request){ request.setAttribute("testScope", "hello,servletAPI"); return "success"; }
使用ModelAndView向request域对象共享数据(最后都会封装到modelAndView) 1 2 3 4 5 6 7 8 9 10 11 12 @RequestMapping("/testModelAndView") public ModelAndView testModelAndView(){ /** * ModelAndView有Model和View的功能 * Model主要用于向请求域共享数据 * View主要用于设置视图,实现页面跳转 、 */ ModelAndView mav = new ModelAndView(); //向请求域共享数据 mav.addObject("testScope", "hello,ModelAndView"); //设置视图,实现页面跳转 mav.setViewName("success"); return mav; }
使用Model向request域对象共享数据 1 2 3 4 5 @RequestMapping("/testModel") public String testModel(Model model){ model.addAttribute("testScope", "hello,Model"); return "success"; }
使用map向request域对象共享数据 1 2 3 4 5 @RequestMapping("/testMap") public String testMap(Map<String, Object> map){ map.put("testScope", "hello,Map"); return "success"; }
使用ModelMap向request域对象共享数据 1 2 3 4 5 @RequestMapping("/testModelMap") public String testModelMap(ModelMap modelMap){ modelMap.addAttribute("testScope", "hello,ModelMap"); return "success"; }
Model、ModelMap、Map的关系 Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的
1 2 3 4 public interface Model{} public class ModelMap extends LinkedHashMap<String, Object> {} public class ExtendedModelMap extends ModelMap implements Model {} public class BindingAwareModelMap extends ExtendedModelMap {}
向session域共享数据 1 2 3 4 5 @RequestMapping("/testSession") public String testSession(HttpSession session){ session.setAttribute("testSessionScope", "hello,session"); return "success"; }
向application域共享数据 1 2 3 4 5 @RequestMapping("/testApplication") public String testApplication(HttpSession session){ ServletContext application = session.getServletContext(); application.setAttribute("testApplicationScope", "hello,application"); return "success"; }
34集搭建工程
m可以单独从在,v也可以