欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2021年08月30日 09:35:57   作者:gonghr  
本文主要介紹了SpringMVC 域?qū)ο蠊蚕頂?shù)據(jù)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

index.html

<a th:href="@{/testModelAndView}" rel="external nofollow" >使用ModelAndView</a>


控制器

 /**
     * ModelAndView有Model和View的功能
     * Model主要用于向請(qǐng)求域共享數(shù)據(jù)
     * View主要用于設(shè)置視圖,實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
     */
    @RequestMapping("/testModelAndView")
    public ModelAndView success(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("username","gonghr");   //向請(qǐng)求域共享數(shù)據(jù)
        modelAndView.setViewName("success");  //設(shè)置視圖名稱,實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
        return modelAndView;  //返回
    }

success.html

sucess
<p th:text="${username}"></p>

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

index.html

<a th:href="@{/testModel}" rel="external nofollow" >使用Model</a> <br>

控制器

 @RequestMapping("/testModel")
    public String testModel(Model model){
        model.addAttribute("company","JLU");
        return "success";
    }

success.html

sucess
<p th:text="${company}"></p> <br>

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

index.html

<a th:href="@{/testMap}" rel="external nofollow" >使用Map</a> <br>

控制器

  @RequestMapping("/testMap")
    public String testMap(Map<String, Object> map){
        map.put("age","Nineteen");
        return "success";
    }

sucess.html

sucess
<p th:text="${age}"></p> <br>

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

index.html

<a th:href="@{/testModelMap}" rel="external nofollow" >使用ModelMap</a> <br>

控制器

 @RequestMapping("/testModelMap")
    public String testModelMap(ModelMap modelMap){
        modelMap.addAttribute("major","software engineering");
        return "success";
    }

success.html

<p th:text="${major}"></p> <br>

Model、ModelMap、Map的關(guān)系

經(jīng)過(guò)測(cè)試發(fā)現(xiàn):除了ModelAndView的實(shí)現(xiàn)類是ModelAndView,Model、MapModelMap 的實(shí)現(xiàn)類都是BindingAwareModelMap。

Model、ModelMap、Map類型的參數(shù)其實(shí)本質(zhì)上都是 BindingAwareModelMap 類型的

class of ModelAndView:  class org.springframework.web.servlet.ModelAndView
class of Model:         class org.springframework.validation.support.BindingAwareModelMap
class of Map:           class org.springframework.validation.support.BindingAwareModelMap
class of ModelMap:      class org.springframework.validation.support.BindingAwareModelMap

閱讀ModeAndView的源碼可以發(fā)現(xiàn),ModeAndViewModelMap是組合關(guān)系。下面是ModeAndView的部分源碼。

public class ModelAndView {

    @Nullable
    private ModelMap model;
   
    public ModelAndView() {
    }
	
    public ModelMap getModelMap() {
        if (this.model == null) {
            this.model = new ModelMap();
        }
        return this.model;
    }
    public ModelAndView addObject(String attributeName, @Nullable Object attributeValue) {
        this.getModelMap().addAttribute(attributeName, attributeValue);
        return this;
    }

當(dāng)ModeAndView調(diào)用addObject()方法時(shí)其實(shí)是調(diào)用ModelMapaddAttribute()方法,本質(zhì)上與ModelMap是一樣的。

各個(gè)類之間的關(guān)系如下:

public interface Model{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}

四種方式本質(zhì)上都是調(diào)用的Model接口中的addAttribute方法

向session域共享數(shù)據(jù)

index.html

<a th:href="@{/testSession}" rel="external nofollow" >使用Session</a> <br>

控制器

 @RequestMapping("/testSession")
    public String testSession(HttpSession session){
        session.setAttribute("message","session scope");
        return "success";
    }

success.html

<p th:text="${session.message}"></p> <br>

向application域共享數(shù)據(jù)

index.html

<a th:href="@{/testApplication}" rel="external nofollow" >使用Application</a> <br>

控制器

@RequestMapping("/testApplication")
    public String testApplication(HttpSession session){
        ServletContext application = session.getServletContext();
        application.setAttribute("testApplication","hello,application");
        return "success";

    }

success.html

<p th:text="${application.testApplication}"></p> <br>

到此這篇關(guān)于SpringMVC 域?qū)ο蠊蚕頂?shù)據(jù)的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringMVC 域?qū)ο蠊蚕頂?shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java Metrics系統(tǒng)性能監(jiān)控工具的使用詳解

    Java Metrics系統(tǒng)性能監(jiān)控工具的使用詳解

    Metrics是一個(gè)Java庫(kù),可以對(duì)系統(tǒng)進(jìn)行監(jiān)控,統(tǒng)計(jì)一些系統(tǒng)的性能指標(biāo)。本文就來(lái)和大家詳細(xì)聊聊這個(gè)工具的具體使用,希望對(duì)大家有所幫助
    2022-11-11
  • spring boot多數(shù)據(jù)源動(dòng)態(tài)切換代碼實(shí)例

    spring boot多數(shù)據(jù)源動(dòng)態(tài)切換代碼實(shí)例

    這篇文章主要介紹了spring boot多數(shù)據(jù)源動(dòng)態(tài)切換代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • Spring?cache源碼深度解析

    Spring?cache源碼深度解析

    緩存用于提升系統(tǒng)的性能,特別適用于一些對(duì)資源需求比較高的操作,下面這篇文章主要給大家介紹了關(guān)于Spring?cache源碼的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • Java詳解AVL樹(shù)的應(yīng)用

    Java詳解AVL樹(shù)的應(yīng)用

    AVL樹(shù)是高度平衡的二叉樹(shù),它的特點(diǎn)是AVL樹(shù)中任何節(jié)點(diǎn)的兩個(gè)子樹(shù)的高度最大差別為1,本文主要給大家介紹了Java如何實(shí)現(xiàn)AVL樹(shù),需要的朋友可以參考下
    2022-07-07
  • java實(shí)現(xiàn)系統(tǒng)捕獲異常發(fā)送郵件案例

    java實(shí)現(xiàn)系統(tǒng)捕獲異常發(fā)送郵件案例

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)系統(tǒng)捕獲異常發(fā)送郵件案例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Java動(dòng)態(tài)代理分析及理解

    Java動(dòng)態(tài)代理分析及理解

    這篇文章主要介紹了Java動(dòng)態(tài)代理分析及理解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Mac Book中Java環(huán)境變量設(shè)置的方法

    Mac Book中Java環(huán)境變量設(shè)置的方法

    本文給大家介紹mac book 中設(shè)置java環(huán)境變量的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下
    2017-04-04
  • java this super使用方法詳解

    java this super使用方法詳解

    Java中this、super關(guān)鍵字的用法簡(jiǎn)單說(shuō)明:super是Java語(yǔ)言的保留字,用來(lái)指向類的超類,本文將詳細(xì)介紹,需要的朋友可以參考下
    2012-12-12
  • springAOP中用joinpoint獲取切入點(diǎn)方法的參數(shù)操作

    springAOP中用joinpoint獲取切入點(diǎn)方法的參數(shù)操作

    這篇文章主要介紹了springAOP中用joinpoint獲取切入點(diǎn)方法的參數(shù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 淺談Java開(kāi)發(fā)架構(gòu)之領(lǐng)域驅(qū)動(dòng)設(shè)計(jì)DDD落地

    淺談Java開(kāi)發(fā)架構(gòu)之領(lǐng)域驅(qū)動(dòng)設(shè)計(jì)DDD落地

    DDD(Domain-Driven Design 領(lǐng)域驅(qū)動(dòng)設(shè)計(jì))是由Eric Evans最先提出,目的是對(duì)軟件所涉及到的領(lǐng)域進(jìn)行建模,以應(yīng)對(duì)系統(tǒng)規(guī)模過(guò)大時(shí)引起的軟件復(fù)雜性的問(wèn)題
    2021-06-06

最新評(píng)論