域对象共享数据

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

@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request) {
    request.setAttribute("key","value");
    return "index";
}

使用ModelView向request域对象中共享数据

@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
    ModelAndView mv = new ModelAndView();
    // 向请求域中共享数据
    mv.addObject("key","value");
    // 设置视图,实现跳转
    mv.setViewName("index");
    return mv;
}

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

@RequestMapping("/testModel")
public String testModel(Model model) {
    model.addAttribute("key","value");
    return "index";
}

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

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

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

@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap) {
    modelMap.addAttribute("key","value");
    return "index";
}

ModelAndViewModelMapModelMap传递数据时都是实例化org.springframework.validation.support.BindingAwareModelMap实现类

//DispatcherServlet源码,将数据封装的部分代码
// Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

java 域对象共享数据的实现

向session域共享数据

@RequestMapping("/testSession")
public String testSession(HttpSession session){
    session.setAttribute("key","value");
    return "index";
}

向application域对象共享数据

@RequestMapping("testApplication")
public String testApplication(HttpSession session){
    ServletContext servletContext = session.getServletContext();
    servletContext.setAttribute("key","value");
    return "index";
}
原文地址:https://blog.csdn.net/zhongjianboy/article/details/129230229

相关文章:

  • 2022-01-13
  • 2022-12-23
  • 2021-11-03
  • 2021-11-29
  • 2021-11-08
猜你喜欢
  • 2021-08-29
  • 2022-02-03
  • 2022-02-02
  • 2022-12-23
  • 2021-04-15
  • 2021-12-29
相关资源
相似解决方案