1 使用servletAPI向requset域对象共享数据
@Controller
public class ScopeController {
//通过servletapi来获取域对象
@RequestMapping("/testRequestByServletAPI")
public String testRequestByServletAPI(HttpServletRequest request){
request.setAttribute("testReuqestScope","hello,servletAPI");
return "success";
}
}
2使用ModelAndView向reuqest域对象共享数据
@RequestMapping("/testModelAndView")
//使用ModelAndView必须使用ModelAndView作为返回值返回
public ModelAndView testModelAndView(){
ModelAndView mov = new ModelAndView();
//处理模型数据,即向请求域request共享数据
mov.addObject("testReuqestScope","hello,ModelAndView");
//设置视图名称
mov.setViewName("success");
return mov;
}
使用Model向reuqest域对象共享数据
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testReuqestScope","hello,model");
return "success";
}
通过map向request域对象共享数据
@RequestMapping("/testMap")
public String testMap(Map<String , Object> map){
//这里我们向map集合中存储的数据就是向request域中共享的数据。
map.put("testReuqestScope","hello,map");
return "success";
}
通过ModelMap向request域对象共享数据
@RequestMapping("/testModelMap")
public String testMap(ModelMap modelMapm){
//这里我们向map集合中存储的数据就是向request域中共享的数据。
modelMapm.put("testReuqestScope","hello,modelmap");
return "success";
}
Model ModelMap Map之间的关系
不管用什么方式共享数据,最后都要在DispathcherServlet中将模型和视图信息封装成一个ModelAndView
向session域共享数据
@RequestMapping("/testSession")
public String testSession(HttpSession httpSession){
httpSession.setAttribute("testSessionScope","hello,session" );
return "success";
}
向application域共享数据
@RequestMapping("/testApplication")
public String testApplication(HttpSession httpSession){
ServletContext application = httpSession.getServletContext();
application.setAttribute("testApplicationScope","hello,application");
return "success";
}