请求域&会话域和应用域共享数据

向请求域共享数据

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

cotroller控制层

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

thymeleaf渲染页面

<!-- auccess.xml -->
<h1>跳转成功</h1>
<p th:text="${testRequestScope}"></p>
<!--页面显示: hello,servletAPI -->

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

ModelAndView包含Model和view的功能:

ModelAndView包含Model和view的功能
Model:向请求域中共享数据
1. 通过ModelAndView时,可以使用其Model功能向请求域共享数据。
view:设置逻辑视图,实现页面跳转
2. 使用View功能设置逻辑视图,但是可控制器方法一定要
将ModelAndView作为方法的返回值。

controller控制层

 @RequestMapping("/test/mva")
    public ModelAndView testMAV(){
        ModelAndView modelAndView=new ModelAndView();
//        向请求域中共享数据
        modelAndView.addObject("testRequestScope","hello,ModelAndView");
//        设置逻辑视图,实现页面跳转
        modelAndView.setViewName("success");
        return modelAndView;
    }

thymeleaf渲染页面

<!-- auccess.xml -->
<h1>跳转成功</h1>
<p th:text="${testRequestScope}"></p>
<!--页面显示:hello,ModelAndView -->

3.使用Model&ModelMap&Map向请求域共享数据

Model和ModelMap和Map的关系

在底层中,这些类型的形参最终都是通过BindingAwareModelMap创建的。

controller层

    @RequestMapping("/test/model")
//    DispatcherServlet调用这个方法的时候帮助我们创建了Model对象
    public String testModel(Model model){
        model.addAttribute("testRequestScope","hello,Model");
        return "success";
    }

    @RequestMapping("/test/modelMap")
//    DispatcherServlet调用这个方法的时候帮助我们创建了Model对象
    public String testModelMap(ModelMap modelMap){
        modelMap.addAttribute("testRequestScope","hello,ModelMap");
        return "success";
    }

    @RequestMapping("/test/map")
//    DispatcherServlet调用这个方法的时候帮助我们创建了Model对象
    public String testMap(Map<String,Object> map){
        map.put("testRequestScope","hello,map");
        return "success";
    }

thymeleaf渲染页面

<!--index.xml-->

<a th:href="@{/test/model}">测试通过Model</a><br>
<a th:href="@{/test/modelMap}">测试通过ModelMap</a>
<a th:href="@{/test/map}">测试通过Map</a>
<!-- auccess.xml -->
<h1>跳转成功</h1>
<p th:text="${testRequestScope}"></p>

会话域和应用域共享数据

这两种方式建议直接使用servletAPI就可以。

相比较:springmvc提供的方式并没有使用servletAPI简单。

session域共享数据

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

application域共享数据

    @RequestMapping("/test/application")
//    servletcontext是可以通过其他域对象获取,比如request也可以等等
    public String testApplication(HttpSession session){
        ServletContext servletContext = session.getServletContext();
        servletContext.setAttribute("testApplicationScope","hello,application");
        return "success";
    }

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值