SpringMVC-共享数据

本文详细介绍了如何在SpringMVC中使用servletAPI、ModelAndView、Model、Map、ModelMap以及session和application域来向HTTP请求域对象共享数据,以便于在页面展示。
摘要由CSDN通过智能技术生成

使用servletapi向request 域对象 共享数据

再controller中编写方法

 //使用servletAPI向request域对象共享数据
 @RequestMapping("/testReq")
 public String req(HttpServletRequest request){
 ​
     request.setAttribute("testReq","hello,servletapi");
 ​
     return "success";
 }

在success.html 中

页面即显示 hello,servletapi​

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

 /**
  * ModelAndView 包含Model 和View的功能
  * Model 主要用于向请求域共享数据
  * View  主要用于设置视图,实现页面跳转
  * @return
  */
 @RequestMapping("/testModelAndView")
 public ModelAndView testModelAndView(){
     ModelAndView mav = new ModelAndView();
     //设置视图名称
     mav.setViewName("success");
     //处理模型数据,即向请求域request共享数据
     mav.addObject("testMV","hello,mv");
     return mav;
 }

在success.html 中 <p th:text="${testMV}"></p>

页面显示: hello,mv

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

 @RequestMapping("/testModel")
 public String testModel(Model model){
     model.addAttribute("testModel","hello,model");
     return "success";
 }

在success.html 中 <p th:text="${testModel}"></p>

页面显示: hello,model

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

 @RequestMapping("/testMap")
 public String testMap(Map<String,Object>map){
     map.put("testMap","hello,map");
     return "success";
 }

在success.html 中 <p th:text="${testMap}"></p>

页面显示: hello,map

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

 @RequestMapping("/testModelMap")
 public String testModelMap(ModelMap map){
     map.put("testModelMap","hello,modelmap");
     return "success";
 }

在success.html 中 <p th:text="${testModelMap}"></p>

页面显示: hello,modelmap

Model, ModelMap,Map的关系

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

 public interface Model{}
 public class ModelMap extends LinkedHashMap<String,Object>{}
 public class ExtendedModelMap extends ModelMap implements Model{}
 public class BindingAwareModelMap extends ExtendedModelMap{}
 ​
 ​

向session域共享数据

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

在success.html 中 <p th:text="${session.tesSession}"></p>

页面显示: hello,session

session 默认失效时间 半小时

向application域共享数据

 
@RequestMapping("/testApplication")
 public String testApplication(HttpSession session){
     ServletContext application = session.getServletContext();
     application.setAttribute("testApplication","hello,Application");
     return "success";
 }

在success.html 中 <p th:text="${application.testApplication}"></p>

页面显示: hello,Application

  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

射手座的程序媛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值