springMVC域对象共享数据

提示:学习记录


Request:一次请求

Session:一次会话(浏览器开启到关闭)

ServletContext(Application):服务器开启到关闭

1、向request域共享数据

使用以下方法向Request请求域共享数据:

  • 原生ServletAPI
  • ModelAndView
  • Model
  • Map<T,T>
  • ModelMap

控制器方法:

    //1、原生ServletAPI向request请求域共享数据
    @RequestMapping("/testServletAPI")
    public String testServletAPI(HttpServletRequest request){
        request.setAttribute("username","jerry");
        request.setAttribute("password",123);
        return "target";
    }
    //2、ModelAndView向request域共享数据
    //需返回ModelAndView对象
    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        ModelAndView modelAndView = new ModelAndView();
        //设置所共享的参数
        modelAndView.addObject("username","jack");
        modelAndView.addObject("password","abc");
        //设置视图名称,即所转发的视图界面
        modelAndView.setViewName("target");
        return modelAndView;
    }

    //3、测试Model向request请求域共享数据
    @RequestMapping("/testModel")
    public String testModel(Model model){
        model.addAttribute("username","harry");
        model.addAttribute("password","kkk");
        return "target";
    }

    //4、测试Map向request请求域共享数据
    @RequestMapping("/testMap")
    public String testMap(Map<String,String> map){
        map.put("username","Bob");
        map.put("password","mmm");
        return "target";
    }

    //5、测试ModelMap向request请求域共享数据
    @RequestMapping("/testModelMap")
    public String testModelMap(ModelMap modelMap){
        modelMap.addAttribute("username","herry");
        modelMap.addAttribute("password","123");
        return "target";
    }

index.html视图层:

<a th:href="@{/testServletAPI}">测试原生ServletAPI向request请求域中共享参数</a><br>
<a th:href="@{/testModelAndView}">测试ModelAndView向request请求域共享参数</a><br>
<a th:href="@{/testModel}">测试Model向request请求域共享参数</a><br>
<a th:href="@{/testModelMap}">测试testModelMap向request请求域共享参数</a><br>

2、向session和application中共享数据

    //向session共享数据
    @RequestMapping("/testSession")
    public String testSession(HttpSession session){
        session.setAttribute("username","lili");
        session.setAttribute("password","uiui");
        return "target";
    }
    //向Application域中共享参数
    @RequestMapping("/testApplication")
    public String testApplication(HttpSession session){
        ServletContext servletContext = session.getServletContext();
        servletContext.setAttribute("username","Moly");
        servletContext.setAttribute("password","oo");
        return "target";
    }

index.html:

<a th:href="@{/testSession}">测试原生ServletAPI向session请求域中共享参数</a><br>
<a th:href="@{/testApplication}">测试向Application请求域中共享参数</a><br>

可以在转发的target.html视图页面显示出所共享的参数username和password:

<!--    获取存储在request域中的数据-->
<p th:text="${username}"></p>
<p th:text="${password}"></p>
<!--    获取存储在session域中的数据-->
<p th:text="${session.username}"></p>
<p th:text="${session.password}"></p>
<!--    获取存储在application域中的数据-->
<p th:text="${application.username}"></p>
<p th:text="${application.password}"></p>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值