SpringMVC-域对象共享数据

一、request域对象共享数据

1.1 通过ServletAPI共享数据

java 复制代码
    @RequestMapping("/servletAPI")
    public String servletAPI(HttpServletRequest request){
        request.setAttribute("requestAttribute","helloworld");
        return "servletAPI";
    }
html 复制代码
<!--通过thymeleaf语法来获取request域对象中的数据,获取request属性时,不需要使用request.
    直接通过属性名获得数据即可-->
<p th:text="${requestAttribute}"></p>

1.2 通过ModelAndView共享数据

java 复制代码
    /**
     * ModelAndView有model和view的功能
     * @return 返回值为ModelAndView对象,交给前端处理器处理
     */
    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        //new一个ModelAndView对象
        ModelAndView modelAndView = new ModelAndView();
        //向请求域中共享数据
        modelAndView.addObject("requestAttribute","helloworld,modelandview");
        //设置视图名称,由thymeleaf解析
        modelAndView.setViewName("testModelAndView");
        return modelAndView;
    }

1.3 通过Model共享数据

java 复制代码
    /**
     *
     * @param model 传入Model对象,以便向request中共享数据
     * @return 返回视图名称
     */
    @RequestMapping("/testModel")
    public String testModel(Model model){
        //向request中共享数据
        model.addAttribute("requestAttribute","helloworld,Model");
        return "testModel";
    }

1.4 通过map共享数据

java 复制代码
    @RequestMapping("/testMap")
    public String testMap(Map<String,Object> map){
        map.put("requestAttribute","helloworld,Map");
        return "testMap";
    }

1.5 通过ModelMap共享数据

java 复制代码
    @RequestMapping("/testModelMap")
    public String testModelMap(ModelMap modelMap){
        modelMap.addAttribute("requestAttribute","helloworld,ModelMap");
        return "testModelMap";
    }

1.6 Model、ModelMap和Map的关系

上面使用的Model、ModelMap和Map类型的参数本质上都是同一个类BindingAwareModelMap的实例。



1.7 ModelAndView

以上方法最后都会使用到ModelAndView对象,所以这些方法本质上都是向ModelAndView中添加数据。

打断点后调试,可以在方法调用栈中看到doDispatch方法,进入这个方法中。

可以看到这个方法中得到了一个ModelAndView对象,接着在这一行打断点,然后进入调试模式,执行上述的所有共享数据的方法,就会发现这些方法都执行了这一行代码,也就是都使用了ModelAndView对象。

二、session域对象共享数据

向session域中共享数据直接使用原生API即可。

java 复制代码
    @RequestMapping("/testSession")
    public String testSession(HttpSession session){
        session.setAttribute("sessionAttribute","helloworld,session");
        return "testSession";
    }

三、context域对象共享数据

向context域对象中共享数据直接使用原生API即可。

java 复制代码
    @RequestMapping("/testContext")
    public String testContext(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        application.setAttribute("contextAttribute","helloworld,context");
        return "testContext";
    }
相关推荐
特立独行的猫A13 小时前
用仓颉语言 cj-tauri 框架,五分钟给娃做了个鸿蒙形近字游戏
java·后端·python
budingxiaomoli20 小时前
Spring统一功能处理
java·后端·spring
Flynn Liang21 小时前
可观测性与成本治理:一次对话的 17 个观测点
java·langchain·可观测性·micrometer·langchain4j·token 成本·llm 监控
金銀銅鐵1 天前
带您看懂字节码指令(三):同一行加法,为什么有四种指令
java·jvm
文丑颜不良啊1 天前
LeetCode2821~2840题目解析
java·leetcode
apcipot_rain1 天前
C++转Java日记(10)
java·开发语言
简 洁 冬冬1 天前
泛型方法写法全面辨析:从「能编译」到「能跑」到「能用」
java·泛型
LGL6030A1 天前
Java学习历程31——初识Spring Boot
java·spring boot·学习
Lansonli1 天前
大数据Spark(八十八):Spark任务资源之Standalone集群测试
java·服务器·spark
budingxiaomoli1 天前
Spring AOP
java·spring·java-ee