最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

index.html

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


控制器

 /**
     * ModelAndView有Model和View的功能
     * Model主要用于向請求域共享數(shù)據(jù)
     * View主要用于設(shè)置視圖,實現(xiàn)頁面跳轉(zhuǎn)
     */
    @RequestMapping("/testModelAndView")
    public ModelAndView success(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("username","gonghr");   //向請求域共享數(shù)據(jù)
        modelAndView.setViewName("success");  //設(shè)置視圖名稱,實現(xiàn)頁面跳轉(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)過測試發(fā)現(xiàn):除了ModelAndView的實現(xiàn)類是ModelAndViewModel、MapModelMap 的實現(xiàn)類都是BindingAwareModelMap

Model、ModelMap、Map類型的參數(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()方法時其實是調(diào)用ModelMapaddAttribute()方法,本質(zhì)上與ModelMap是一樣的。

各個類之間的關(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ù)的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringMVC 域?qū)ο蠊蚕頂?shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中Arrays的介紹及使用方法示例

    Java中Arrays的介紹及使用方法示例

    這篇文章主要給大家介紹了關(guān)于Java中Arrays及使用方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java面試題目集錦

    Java面試題目集錦

    本文是小編日常收集整理的java面試題目,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • Mybatis Plus使用條件構(gòu)造器增刪改查功能的實現(xiàn)方法

    Mybatis Plus使用條件構(gòu)造器增刪改查功能的實現(xiàn)方法

    這篇文章主要介紹了Mybatis-Plus使用條件構(gòu)造器增刪改查,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-05-05
  • Java并發(fā)Map面試線程安全數(shù)據(jù)結(jié)構(gòu)全面分析

    Java并發(fā)Map面試線程安全數(shù)據(jù)結(jié)構(gòu)全面分析

    本文將探討如何在Java中有效地應(yīng)對這些挑戰(zhàn),介紹一種強(qiáng)大的工具并發(fā)Map,它能夠幫助您管理多線程環(huán)境下的共享數(shù)據(jù),確保數(shù)據(jù)的一致性和高性能,深入了解Java中的并發(fā)Map實現(xiàn),包括ConcurrentHashMap和ConcurrentSkipListMap,及相關(guān)知識點
    2023-09-09
  • springboot如何使用yml文件方式配置shardingsphere

    springboot如何使用yml文件方式配置shardingsphere

    這篇文章主要介紹了springboot如何使用yml文件方式配置shardingsphere問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Java Kafka實現(xiàn)優(yōu)先級隊列的示例詳解

    Java Kafka實現(xiàn)優(yōu)先級隊列的示例詳解

    在分布式系統(tǒng)中,消息隊列是一種常見的異步通信機(jī)制,而優(yōu)先級隊列則是消息隊列的一種特殊形式,下面我們來看看如何利用Kafka實現(xiàn)優(yōu)先級隊列吧
    2025-03-03
  • java實現(xiàn)多線程交替打印兩個數(shù)

    java實現(xiàn)多線程交替打印兩個數(shù)

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)多線程交替打印兩個數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • 解決Jenkins集成SonarQube遇到的報錯問題

    解決Jenkins集成SonarQube遇到的報錯問題

    本文給大家分享Jenkins集成SonarQube遇到的報錯問題及解決方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2021-07-07
  • java字符緩沖流面試精講

    java字符緩沖流面試精講

    這篇文章主要為大家介紹了java中字符緩沖流面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • java的arrays數(shù)組排序示例分享

    java的arrays數(shù)組排序示例分享

    排序算法,基本的高級語言都有一些提供。C語言有qsort()函數(shù),C++有sort()函數(shù),java語言有Arrays類(不是Array)。用這些排序時,都可以寫自己的排序規(guī)則
    2014-02-02

最新評論

马山县| 察隅县| 吕梁市| 石楼县| 玉环县| 那坡县| 峨边| 潞西市| 特克斯县| 伊春市| 九台市| 黔南| 铜梁县| 平南县| 南靖县| 会泽县| 福泉市| 措美县| 苍梧县| 阿鲁科尔沁旗| 乐亭县| 贵州省| 锡林郭勒盟| 梁河县| 来凤县| 永济市| 浦北县| 南华县| 郎溪县| 德令哈市| 新竹市| 睢宁县| 新沂市| 沁源县| 白沙| 新安县| 探索| 扬州市| 泰宁县| 老河口市| 大英县|