Spring MVC中的ModelAndView中的模型是什么? [英] What is Model in ModelAndView from Spring MVC?

查看:130
本文介绍了Spring MVC中的ModelAndView中的模型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有此基本功能

protected ModelAndView handleRequestInternal(...) {
    ...
    return new ModelAndView("welcomePage", "WelcomeMessage", message);
}

我知道这将返回modelandView. 我知道welcomePage是我的视图名,所以这意味着将调用welcomepage.jsp之类的东西.

I know that this will return modelandView. I know that welcomePage is my viewname so that means something like welcomepage.jsp will get called.

但是我对模型部分感到困惑. 什么是WelcomeMessagemessage,以及在这种情况下模型如何工作?

But I am confused with what is Model part. What is WelcomeMessage and message and how Model work in that scenario?

推荐答案

模型提供了一个占位符,用于保存要在视图上显示的信息.可以是上面的示例中的字符串,也可以是包含一堆属性的对象.

The model presents a placeholder to hold the information you want to display on the view. It could be a string, which is in your above example, or it could be an object containing bunch of properties.

示例1

如果有...

return new ModelAndView("welcomePage","WelcomeMessage","Welcome!");

...然后在您的jsp中显示该消息,您将执行以下操作:-

... then in your jsp, to display the message, you will do:-

Hello Stranger! ${WelcomeMessage} // displays Hello Stranger! Welcome!

示例2

如果有...

MyBean bean = new MyBean();
bean.setName("Mike!");
bean.setMessage("Meow!");

return new ModelAndView("welcomePage","model",bean);

...然后在您的jsp中,您可以执行以下操作:-

... then in your jsp, you can do:-

Hello ${model.name}! {model.message} // displays Hello Mike! Meow!

这篇关于Spring MVC中的ModelAndView中的模型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆