• 域对象共享数据


    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
  • 相关阅读:
    【OFDM系列5】单输入单输出OFDM(SISO-OFDM)多径信道迫零(ZF)和最小均方误差(MMSE)均衡器原理和公式推导
    Fiddler工具 — 19.Fiddler抓包HTTPS请求(二)
    idea maven 构建本地jar包及pom文件
    11+ chrome高级调试技巧,学会效率直接提升666%
    容器编排学习(一)k8s集群管理
    2023年显著性检测论文及代码汇总(4)
    Synchronized
    STM32单片机入门学习(五)-按钮控制LED
    想学嵌入式?要不一起玩 Arduino 吧
    python环境安装教程
  • 原文地址:https://blog.csdn.net/m0_56336875/article/details/126091946