人生到头来,就是不断放下,但永远最令人心痛的就|

durtime

园龄:4年8个月粉丝:10关注:1

springmvc05

域对象共享数据

1、使用ServletAPI向request域对象共享数据

@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
   request.setAttribute("testScope", "hello,servletAPI");
   return "success";
}

2、使用ModelAndView向request域对象共享数据

@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
   /**
    * ModelAndView有Model和View的功能
    * Model主要用于向请求域共享数据
    * View主要用于设置视图,实现页面跳转
    */
   ModelAndView mav = new ModelAndView();
   //向请求域共享数据
   mav.addObject("testScope", "hello,ModelAndView");
   //设置视图,实现页面跳转
   mav.setViewName("success");
   return mav;
}

3、使用Model向request域对象共享数据

@RequestMapping("/testModel")
public String testModel(Model model){
   model.addAttribute("testScope", "hello,Model");
   return "success";
}

4、使用map向request域对象共享数据

@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
   map.put("testScope", "hello,Map");
   return "success";
}

5、使用ModelMap向request域对象共享数据

@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
   modelMap.addAttribute("testScope", "hello,ModelMap");
   return "success";
}

6、Model、ModelMap、Map的关系

Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的

public interface Model{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}

7、向session域共享数据

@RequestMapping("/testSession")
public String testSession(HttpSession session){
   session.setAttribute("testSessionScope", "hello,session");
   return "success";
}

8、向application域共享数据

@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
ServletContext application = session.getServletContext();
   application.setAttribute("testApplicationScope", "hello,application");
   return "success";
}



本文作者:durtime

本文链接:https://www.cnblogs.com/durtime/p/16042050.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   durtime  阅读(19)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开