美文网首页
五、域对象共享数据

五、域对象共享数据

作者: 不减肥到一百三不改名字 | 来源:发表于2021-11-16 21:37 被阅读0次

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

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

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

@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
    /**
     * ModelAndView有Model和View的功能
     * Model主要用于向请求域共享数据
     * View主要用于设置视图,实现页面跳转
     */
    ModelAndView mav = new ModelAndView();
    //向请求域共享数据
    mav.addObject("testScope", "hello,ModelAndView");
    //设置视图,实现页面跳转
    mav.setViewName("success");
    return mav;
}

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

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

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

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

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

@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
    modelMap.addAttribute("testScope", "hello,ModelMap");
    return "success";
}

6、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 {}

7、向session域共享数据

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

8、向application域共享数据

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

相关文章

  • 五、域对象共享数据

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

  • servlet作用域对象

    servlet三大作用域对象:request、session、application目的:共享数据 作用域对象如何...

  • 20.数据共享

    Web组件(Servlet/JSP)的数据共享得需要作用域对象. 作用域对象存在的意义: 在多个Web组件之间共享...

  • 26.九大内置对象

    JSP的内置对象 JSP的四大作用域: 作用域对象就只能在自己的作用范围之内共享数据. SP中隐式对象的名称 作用...

  • 跟诸子学游戏 学习服务器3

    Servlet的三大作用域对象: 目的:共享数据. request: 每一次请求都是一个新的request对象,如...

  • ES 语法

    一、常量 二、作用域 三、箭头函数 this 为定义时的对象 四、默认参数 五、数据保护 六、对象代理

  • JS Ajax

    Ajax的核心是XMLHttpRequest对象 XMLHttpRequest对象 跨域资源共享问题 Ajax的扩...

  • Servlet(作用)域对象

    Servlet相互调用的时候传输数据(作用)域对象1、有哪些域对象ServletRequest、HttpSessi...

  • 跨源网络访问

    链接:浏览器的同源策略链接:跨域资源共享链接:跨域共享数据的十种方法链接:前端跨域问题及其解决方案 广义的跨域:1...

  • 黑猴子的家:JavaWeb 之 Session

    1、Session 概述 Session也是一个域对象,可以在自身的属性域中保存数据,在一定范围内共享。 2、Se...

网友评论

      本文标题:五、域对象共享数据

      本文链接:https://www.haomeiwen.com/subject/wdrialtx.html