SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)示例詳解
SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)
一、域?qū)ο?/h3>
1. 域?qū)ο蟮淖饔?/h4>
就是在一定范圍內(nèi)可以共享數(shù)據(jù),通常有 3 種:
- request: 一次請求,多個資源共享數(shù)據(jù)
- session: 默認一次會話,多個請求,多個資源共享數(shù)據(jù)
- servletContext: 一個應(yīng)用,多個會話,多個請求,多個資源共享同一份數(shù)據(jù)
2. 域?qū)ο笊芷?/h4>
- request: 創(chuàng)建-請求開始,銷毀-響應(yīng)結(jié)束
- session: 創(chuàng)建-第一次調(diào)用,銷毀- tomcat超時三十分鐘(默認)、或者手動調(diào)用invalidate()、或者服務(wù)器非正常關(guān)閉
- servletContext: 創(chuàng)建-啟動時,銷毀-關(guān)閉時
3. 使用原則
在滿足需求的前提下,能選擇小范圍的就不選擇大范圍的。
舉例,一個查詢列表場景,這種一般放在 request 域中,當(dāng)然了放在更大的2個里面也可以,但是沒必要。
因為除了查詢還同時會存在增刪改的功能,所以我們要每次去查詢一次才可以保證數(shù)據(jù)是最新的,這時候如果放在更大的域比如 session 中,那么在一次請求中只會用到一次,其他時候這個空間就浪費掉了。
二、向域?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",最后這個請求轉(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)在訪問http://localhost:8080/springmvc/testServletAPI,

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

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

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

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

獲取成功。
Model、ModelMap、Map的關(guān)系
Model、ModelMap、Map類型的參數(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 頁面也需要修改了,通過 session.testSessionScope獲取數(shù)據(jù)展示:
... ...
<p th:text="${session.testSessionScope}"></p>
... ...重新部署訪問http://localhost:8080/springmvc/testSession:

3. 向 application 域共享數(shù)據(jù)
其實這里的 application 指的就是 ServletContext 對象,用 session.getServletContext() 來獲取即可:
@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>... ...
重新部署訪問http://localhost:8080/springmvc/testApplication:

獲取成功。
感謝《尚硅谷》的學(xué)習(xí)資源。
以上就是SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)示例詳解的詳細內(nèi)容,更多關(guān)于SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
elasticsearch索引index之engine讀寫控制結(jié)構(gòu)實現(xiàn)
這篇文章主要為大家介紹了elasticsearch索引index之engine讀寫控制結(jié)構(gòu)實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04
IntelliJ IDEA 安裝教程2019.09.23(最新版)
本文通過圖文并茂的形式給大家介紹了IntelliJ IDEA 安裝教程2019.09.23最新版,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10

