1.servletAPI向request域对象共享数据(通常不用)
@RequestMapping("/testRequestByServletAPI")
public String testRequestByServletAPI(HttpServletRequest request){
return "success";
}
2.ModelAndView向request域对象共享数据
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
ModelAndView mav = new ModelAndView();
//向请求域request共享数据
mav.addObject("ModelAndView", "hello,ModelAndView");
//设置视图的名称
mav.setViewName("success");
return mav;
}
3.Model向request域对象共享数据
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("Model", "hello,model");
return "success";
}
4.ModelMap向request域对象共享数据
@RequestMapping("/ModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("ModelMap", "hello,ModelMap");
return "success";
}
5、map向request域对象共享数据
@RequestMapping("/Map")
public String testMap(Map<String, Object> map){
map.put("Map", "hello,Map");
return "success";
}
6.Model、ModelMap、Map的关系:
他们都可以用来存放数据到页面,并且都在请求域(requestScope)中
7.session域共享数据
@RequestMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("testSessionScope", "session");
return "success";
}
8.application域共享数据
RequestMapping("/testApplication")
public String testApplication(HttpSession session){
ServletContext application = session.getServletContext();
application.setAttribute("testApplicationScope", "hello,application");
return "success";
}