【SpringMVC】域对象共享数据

使用ModelAndView向request域对象共享数据

index.html

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

image
控制器

    /**
     * ModelAndView有Model和View的功能
     * Model主要用于向请求域共享数据
     * View主要用于设置视图,实现页面跳转
     */
    @RequestMapping("/testModelAndView")
    public ModelAndView success(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("username","gonghr");   //向请求域共享数据
        modelAndView.setViewName("success");  //设置视图名称,实现页面跳转
        return modelAndView;  //返回
    }

success.html

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

image

使用Model向request域对象共享数据

index.html

<a th:href="@{/testModel}">使用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>

image

使用map向request域对象共享数据

index.html

<a th:href="@{/testMap}">使用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>

image

使用ModelMap向request域对象共享数据

index.html

<a th:href="@{/testModelMap}">使用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>

image

Model、ModelMap、Map的关系

经过测试发现:除了ModelAndView的实现类是ModelAndViewModelMapModelMap 的实现类都是BindingAwareModelMap

ModelModelMapMap类型的参数其实本质上都是 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的源码可以发现,ModeAndViewModelMap是组合关系。下面是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;
    }

ModeAndView调用addObject()方法时其实是调用ModelMapaddAttribute()方法,本质上与ModelMap是一样的。

各个类之间的关系如下:

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

image

四种方式本质上都是调用的Model接口中的addAttribute方法

向session域共享数据

index.html

<a th:href="@{/testSession}">使用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>

image

向application域共享数据

index.html

<a th:href="@{/testApplication}">使用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>

image

本文作者:gonghr

本文链接:https://www.cnblogs.com/gonghr/p/15202176.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   gonghr  阅读(291)  评论(0编辑  收藏  举报
评论
收藏
关注
推荐
深色
回顶
收起
点击右上角即可分享
微信分享提示
  1. 1 Whenever you are ONE OK ROCK
Whenever you are - ONE OK ROCK
00:00 / 00:00
An audio error has occurred.

作词 : TAKA

作曲 : TAKA

I'm telling you

Tonight tonight

You are my angel

爱してるよ

爱してるよ

2人は一つに

Tonight tonight

I just say…

Wherever you are I always make you smile

Wherever you are I'm always by your side

Whatever you say 君を思う気持ち

I promise you forever right now

I don't need a reason

I don't need a reason

I just want you baby

Alright alright

Day after day

この先长いことずっと

この先长いことずっと

どうかこんな仆とずっと

死ぬまで Stay with me

We carry on…

Wherever you are I always make you smile

Wherever you are I'm always by your side

Whatever you say' 君を思う気持ち

I promise you forever right now

Wherever you are I never make you cry

Wherever you are I'never say goodbye

Whatever you say 君を思う気持ち

I promise you forever right now

仆らが出逢った日は2人にとって

一番目の记念すべき日だね

そして今日という日は2人にとって

二番目の记念すべき日だね

心から爱せる人

心から爱せる人

心から爱しい人

この仆の爱の真ん中には

いつも君(きみ)がいるから

Wherever you are I always make you smile

Wherever you are I'm always by your side

Whatever you say 君を思う気持ち

I promise you forever right now

Wherever you are wherever you are

Wherever you are

おわり

おわり

おわり