溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)怎么實(shí)現(xiàn)

發(fā)布時(shí)間:2022-05-30 09:45:44 來(lái)源:億速云 閱讀:230 作者:iii 欄目:開(kāi)發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)怎么實(shí)現(xiàn)”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)怎么實(shí)現(xiàn)”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。

SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)

一、域?qū)ο?/h4>

1. 域?qū)ο蟮淖饔?/h5>

就是在一定范圍內(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ū)ο笊芷?/h5>
  • request: 創(chuàng)建-請(qǐng)求開(kāi)始,銷毀-響應(yīng)結(jié)束

  • session: 創(chuàng)建-第一次調(diào)用,銷毀- tomcat超時(shí)三十分鐘(默認(rèn))、或者手動(dòng)調(diào)用invalidate()、或者服務(wù)器非正常關(guān)閉

  • servletContext: 創(chuàng)建-啟動(dòng)時(shí),銷毀-關(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,

SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)怎么實(shí)現(xiàn)

可以看到展示出了獲取到的 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:

SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)怎么實(shí)現(xiàn)

獲取成功。

(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è)試一下:

SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)怎么實(shí)現(xiàn)

獲取成功。

(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è)試一下:

SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)怎么實(shí)現(xiàn)

獲取成功。

(5)使用 ModelMap 向 request 域?qū)ο蠊蚕頂?shù)據(jù)

@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
    modelMap.addAttribute("testScope", "hello,ModelMap");
    return "success";
}

測(cè)試一下:

SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)怎么實(shí)現(xiàn)

獲取成功。

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:

SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)怎么實(shí)現(xiàn)

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:

SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)怎么實(shí)現(xiàn)

獲取成功。

讀到這里,這篇“SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)怎么實(shí)現(xiàn)”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI