java 域?qū)ο蠊蚕頂?shù)據(jù)的實(shí)現(xiàn)
域?qū)ο蠊蚕頂?shù)據(jù)
使用ServletAPI向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testServletAPI") public String testServletAPI(HttpServletRequest request) { request.setAttribute("key","value"); return "index"; }
使用ModelView向request域?qū)ο笾泄蚕頂?shù)據(jù)
@RequestMapping("/testModelAndView") public ModelAndView testModelAndView(){ ModelAndView mv = new ModelAndView(); // 向請(qǐng)求域中共享數(shù)據(jù) mv.addObject("key","value"); // 設(shè)置視圖,實(shí)現(xiàn)跳轉(zhuǎn) mv.setViewName("index"); return mv; }
使用Model向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testModel") public String testModel(Model model) { model.addAttribute("key","value"); return "index"; }
使用map向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testMap") public String testMap(Map<String,Object> map) { map.put("key","value"); return "index"; }
使用ModelMap向request域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testModelMap") public String testModelMap(ModelMap modelMap) { modelMap.addAttribute("key","value"); return "index"; }
ModelAndView
、Model
、Map
、ModelMap
傳遞數(shù)據(jù)時(shí)都是實(shí)例化org.springframework.validation.support.BindingAwareModelMap
實(shí)現(xiàn)類
//DispatcherServlet源碼,將數(shù)據(jù)封裝的部分代碼 // Actually invoke the handler. mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
向session域共享數(shù)據(jù)
@RequestMapping("/testSession") public String testSession(HttpSession session){ session.setAttribute("key","value"); return "index"; }
向application域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("testApplication") public String testApplication(HttpSession session){ ServletContext servletContext = session.getServletContext(); servletContext.setAttribute("key","value"); return "index"; }
到此這篇關(guān)于java 域?qū)ο蠊蚕頂?shù)據(jù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)java 域?qū)ο蠊蚕頂?shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java的字符串中對(duì)子字符串的查找方法總結(jié)
這篇文章主要介紹了Java的字符串中對(duì)子字符串的查找方法總結(jié),是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-11-11在springboot項(xiàng)目中同時(shí)接收文件和多個(gè)參數(shù)的方法總結(jié)
在開發(fā)接口中,遇到了需要同時(shí)接收文件和多個(gè)參數(shù)的情況,可以有多種方式實(shí)現(xiàn)文件和參數(shù)的同時(shí)接收,文中給大家介紹了兩種實(shí)現(xiàn)方法,感興趣的同學(xué)跟著小編一起來看看吧2023-08-08SpringBoot使用JWT實(shí)現(xiàn)登錄驗(yàn)證的方法示例
這篇文章主要介紹了SpringBoot使用JWT實(shí)現(xiàn)登錄驗(yàn)證的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06java Arrays快速打印數(shù)組的數(shù)據(jù)元素列表案例
這篇文章主要介紹了java Arrays快速打印數(shù)組的數(shù)據(jù)元素列表案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09解決Error:(5,55)java:程序包org.springframework.cloud.netflix.eure
這篇文章主要介紹了解決Error:(5,55)java:程序包org.springframework.cloud.netflix.eureka.server不存在問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11mybatis多數(shù)據(jù)源動(dòng)態(tài)切換的完整步驟
這篇文章主要給大家介紹了關(guān)于mybatis多數(shù)據(jù)源動(dòng)態(tài)切換的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11IDEA安裝lombok插件設(shè)置Enable Annotation Processing后編譯依然報(bào)錯(cuò)解決方法
這篇文章主要介紹了IDEA安裝lombok插件設(shè)置Enable Annotation Processing后編譯依然報(bào)錯(cuò)解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04