java 域对象共享数据的实现
域对象共享数据
使用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";
}
ModelAndView、Model、Map、ModelMap传递数据时都是实例化org.springframework.validation.support.BindingAwareModelMap实现类
//DispatcherServlet源码,将数据封装的部分代码 // Actually invoke the handler. mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

向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";
}
到此这篇关于java 域对象共享数据的实现的文章就介绍到这了,更多相关java 域对象共享数据内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
postman中参数和x-www-form-urlencoded传值的区别及说明
在Postman中,参数传递有多种方式,其中params和x-www-form-urlencoded最为常用,Params主要用于URL中传递查询参数,适合GET请求和非敏感数据,其特点是将参数作为查询字符串附加在URL末尾,适用于过滤和排序等操作2024-09-09
Mybatis-Plus中分页插件PaginationInterceptor的使用
我们在开发的过程中,经常会遇到分页操作,本文主要介绍了Mybatis-Plus中分页插件PaginationInterceptor的使用,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧2023-06-06


最新评论