SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)示例詳解
SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)
一、域?qū)ο?/h3>
1. 域?qū)ο蟮淖饔?/h4>
就是在一定范圍內(nèi)可以共享數(shù)據(jù),通常有 3 種:
- request: 一次請(qǐng)求,多個(gè)資源共享數(shù)據(jù)
- session: 默認(rèn)一次會(huì)話,多個(gè)請(qǐng)求,多個(gè)資源共享數(shù)據(jù)
- servletContext: 一個(gè)應(yīng)用,多個(gè)會(huì)話,多個(gè)請(qǐng)求,多個(gè)資源共享同一份數(shù)據(jù)
2. 域?qū)ο笊芷?/h4>
- request: 創(chuàng)建-請(qǐng)求開(kāi)始,銷(xiāo)毀-響應(yīng)結(jié)束
- session: 創(chuàng)建-第一次調(diào)用,銷(xiāo)毀- tomcat超時(shí)三十分鐘(默認(rèn))、或者手動(dòng)調(diào)用invalidate()、或者服務(wù)器非正常關(guān)閉
- servletContext: 創(chuàng)建-啟動(dòng)時(shí),銷(xiāo)毀-關(guān)閉時(shí)
3. 使用原則
在滿足需求的前提下,能選擇小范圍的就不選擇大范圍的。
舉例,一個(gè)查詢列表場(chǎng)景,這種一般放在 request 域中,當(dāng)然了放在更大的2個(gè)里面也可以,但是沒(méi)必要。
因?yàn)槌瞬樵冞€同時(shí)會(huì)存在增刪改的功能,所以我們要每次去查詢一次才可以保證數(shù)據(jù)是最新的,這時(shí)候如果放在更大的域比如 session 中,那么在一次請(qǐng)求中只會(huì)用到一次,其他時(shí)候這個(gè)空間就浪費(fèi)掉了。
二、向域?qū)ο蠊蚕頂?shù)據(jù)
1. 向 request 域?qū)ο蠊蚕頂?shù)據(jù)
(1)使用 servletAPI 向 request 域?qū)ο蠊蚕頂?shù)據(jù)
@Controller
public class ScopeController {
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope", "hello,servletAPI");
return "success";
}
}在控制器方法里,設(shè)置 request 域?qū)ο笾械臄?shù)據(jù),key 是testScope,value是"hello,servletAPI",最后這個(gè)請(qǐng)求轉(zhuǎn)發(fā)到了 success.html 上。
那么在 success.html 中就可以獲得域?qū)ο笾械臄?shù)據(jù)了,直接使用 key:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>success</p>
<p th:text="${testScope}"></p>
</body>
</html>現(xiàn)在訪問(wèn)http://localhost:8080/springmvc/testServletAPI,

可以看到展示出了獲取到的 request 域中共享的數(shù)據(jù)。
(2)使用 ModelAndView 向 request 域?qū)ο蠊蚕頂?shù)據(jù)
ModelAndView 有 Model和 View的功能,Model主要用于向請(qǐng)求域共享數(shù)據(jù),View主要用于設(shè)置視圖,實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)。這種也是 springMVC 建議使用的方式。
注意控制器方法必須要返回 ModelAndView 類型的對(duì)象。
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
/**
* ModelAndView有Model和View的功能
* Model主要用于向請(qǐng)求域共享數(shù)據(jù)
* View主要用于設(shè)置視圖,實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
*/
ModelAndView mav = new ModelAndView();
//向請(qǐng)求域共享數(shù)據(jù)
mav.addObject("testScope", "hello,ModelAndView");
//設(shè)置視圖,實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
mav.setViewName("success");
return mav;
}訪問(wèn)一下http://localhost:8080/springmvc/testModelAndView:

獲取成功。
(3)使用 Model 向 request 域?qū)ο蠊蚕頂?shù)據(jù)
這里的 Model 其實(shí)就是上面 ModelAndView 中的 Model 。
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}測(cè)試一下:

獲取成功。
(4)使用 map 向 request 域?qū)ο蠊蚕頂?shù)據(jù)
還可以使用 map 集合來(lái)實(shí)現(xiàn),這時(shí)候向 map 集合中存放的數(shù)據(jù),就是要共享的數(shù)據(jù):
@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
map.put("testScope", "hello,Map");
return "success";
}測(cè)試一下:

獲取成功。
(5)使用 ModelMap 向 request 域?qū)ο蠊蚕頂?shù)據(jù)
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("testScope", "hello,ModelMap");
return "success";
}測(cè)試一下:

獲取成功。
Model、ModelMap、Map的關(guān)系
Model、ModelMap、Map類型的參數(shù)其實(shí)本質(zhì)上都是 BindingAwareModelMap 類型的。
分別查看源碼可知如下關(guān)系:
public interface Model{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}2. 向 session 域共享數(shù)據(jù)
直接使用 servlet 原生的 HttpSession 即可:
@RequestMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("testSessionScope", "hello,session");
return "success";
}success 頁(yè)面也需要修改了,通過(guò) session.testSessionScope獲取數(shù)據(jù)展示:
... ...
<p th:text="${session.testSessionScope}"></p>
... ...重新部署訪問(wèn)http://localhost:8080/springmvc/testSession:

3. 向 application 域共享數(shù)據(jù)
其實(shí)這里的 application 指的就是 ServletContext 對(duì)象,用 session.getServletContext() 來(lái)獲取即可:
@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
ServletContext application = session.getServletContext();
application.setAttribute("testApplicationScope", "hello,application");
return "success";
}success.html 修改:
... ...<p th:text="${application.testApplicationScope}"></p>... ...
重新部署訪問(wèn)http://localhost:8080/springmvc/testApplication:

獲取成功。
感謝《尚硅谷》的學(xué)習(xí)資源。
以上就是SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)示例詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring Cloud Ribbon客戶端詳細(xì)介紹
Spring Cloud Ribbon 是一套基于 Netflix Ribbon 實(shí)現(xiàn)的客戶端負(fù)載均衡和服務(wù)調(diào)用工具。通過(guò)Spring Cloud的封裝,可以讓我們輕松地將面向服務(wù)的REST模版請(qǐng)求自動(dòng)轉(zhuǎn)換成客戶端負(fù)載均衡的服務(wù)調(diào)用2022-09-09
elasticsearch索引index之engine讀寫(xiě)控制結(jié)構(gòu)實(shí)現(xiàn)
這篇文章主要為大家介紹了elasticsearch索引index之engine讀寫(xiě)控制結(jié)構(gòu)實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
淺析Java IO相關(guān)知識(shí)點(diǎn)
本篇文章給大家分享了關(guān)于java io的一些相關(guān)知識(shí)點(diǎn)以及相關(guān)內(nèi)容,對(duì)此有需要的朋友可以學(xué)習(xí)參考下。2018-05-05
IntelliJ IDEA 安裝教程2019.09.23(最新版)
本文通過(guò)圖文并茂的形式給大家介紹了IntelliJ IDEA 安裝教程2019.09.23最新版,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10

