@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("key1", "value1");
return "success";
}
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("key1", "value1");
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("key1", "value1");
return "success";
}
@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
map.put("key1", "value1");
return "success";
}
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("key1", "value1");
return "success";
}
@RequestMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("key1", "value1");
return "success";
}
@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
ServletContext application = session.getServletContext();
application.setAttribute("key1", "value1");
return "success";
}
request (HttpServletRequest 类):一次请求内有效
session (HttpSession 类):一个会话范围内有效(跟服务器关闭没有关系,只跟浏览器是否关闭有关系)
application (ServletContext 类): 整个 web 工程范围内都有效(只跟服务器是否关闭有关系)