• 域对象共享数据


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

    Spring MVC 可以在控制器方法中直接获取 Servlet 原生 Web 资源,只需要在方法定义时添加HttpServletRequest ⼊参即可,在方法体中直接使用request 对象。

    @RequestMapping("/test/servletAPI")
        public String testSevletAPI(HttpServletRequest request) {
            request.setAttribute("testRequestScope","hello,servletAPI");
            return "success";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

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

    与 Map 或者 Model 不同的是,ModelAndView 不但包含业务数据,同时也封装了视图信息,如果使用ModelAndView 来处理业务数据,控制器方法的返回值必须是 ModelAndView 对象。

    @RequestMapping("/test/modelandview")
        public ModelAndView testModelAndView() {
            ModelAndView mav=new ModelAndView();
            /**
             * ModelAndView包含Model和View的功能
             * Model:向请求域中共享数据
             * View:设置逻辑视图实现页面跳转
             */
            //向请求域中共享数据
            mav.addObject("testRequestScope","hello,ModelAndView");
            //设置逻辑视图实现页面跳转
            mav.setViewName("success");
            System.out.println(mav.getClass().getName());
            return mav;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

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

    @RequestMapping(value="/test/model")
        public String testModel(Model model) {
            model.addAttribute("testRequestScope","hello,model");
            System.out.println(model.getClass().getName());
            //org.springframework.validation.support.BindingAwareModelMap
            return "success";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

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

    Spring MVC 在调⽤控制器方法之前会创建⼀个隐含对象作为业务数据的存储容器,设置控制器⽅法的⼊参为 Map 类型,Spring MVC 会将隐含对象的引用传递给⼊参。

    @RequestMapping("/test/map")
        public String testMap(Map<String,Object> map) {
            map.put("testRequestScope","hello,map");
            System.out.println(map.getClass().getName());
            //org.springframework.validation.support.BindingAwareModelMap
    
            return "success";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

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

    @RequestMapping("/test/modelmap")
        public String testModelMap(ModelMap modelMap) {
            modelMap.addAttribute("testRequestScope","hello,modelmap");
            System.out.println(modelMap.getClass().getName());
            //org.springframework.validation.support.BindingAwareModelMap
            return "success";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    补充:Model、ModelMap、Map的关系

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

    public interface Model{} public class ModelMap extends
    LinkedHashMap {} public class ExtendedModelMap extends
    ModelMap implements Model {} public class BindingAwareModelMap extends
    ExtendedModelMap {}

    6.向session域共享数据

    @RequestMapping("/test/session")
        public String testSession(HttpSession session) {
            session.setAttribute("testRequestScope","hello,session");
            return "success";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    7.向application域共享数据

    @RequestMapping("/test/application")
        public String testApplication(HttpSession session) {
            ServletContext appliation=session.getServletContext();
            appliation.setAttribute("testRequestScope","hello,application");
            return "success";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    C++ 重载运算符,语法+示例,非常详细!!!
    <b><strong>,<i><em>标签的区别
    java面试题整理《微服务篇》二
    KMP子串匹配算法
    ADManager Plus 移动应用程序,随时随地管理 Active Directory
    向量数据库——AI时代的基座
    SystemVerilog Assertions应用指南 Chapter 1.14蕴含操作符
    Linux系统CH347应用—概述
    virtualbox虚拟机安装在笔记本上使用WIFI无法上网
    Windows OpenGL ES 图像色彩替换
  • 原文地址:https://blog.csdn.net/m0_56336875/article/details/126091946