这篇“java域对象共享数据如何实现”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“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域对象共享数据如何实现”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注云搜网行业资讯频道。