查看spring的帮助文档得到下面信息:
org.springframework.web.servlet
Class ModelAndView
java.lang.Object org.springframework.web.servlet.ModelAndView
-
public class ModelAndView
- extends Object
Holder for both Model and View in the web MVC framework. Note that these are entirely distinct. This class merely holds both to make it possible for a controller to return both model and view in a single return value.
Class to represent a model and view returned by a handler used by a DispatcherServlet. The view can take the form of a reference to a View object, or a String view name which will need to be resolved by a ViewResolver object. The model is a Map, allowing the use of multiple data objects keyed by name.
- Author:
- Rod Johnson, Juergen Hoeller
- See Also:
-
DispatcherServlet
,ViewResolver
,HandlerAdapter.handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
,Controller.handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
Constructor Summary | |
---|---|
ModelAndView
()
Default constructor for bean-style usage: populating bean properties instead of passing in constructor arguments. |
|
ModelAndView
(
String
viewName)
Convenient constructor when there is no model data to expose. |
|
ModelAndView
(
String
viewName,
Map
model)
Creates new ModelAndView given a view name and a model. |
|
ModelAndView
(
String
viewName,
String
modelName,
Object
modelObject)
Convenient constructor to take a single model object. |
|
ModelAndView
(
View
view)
Convenient constructor when there is no model data to expose. |
|
ModelAndView
(
View
view,
Map
model)
Creates new ModelAndView given a View object and a model. |
|
ModelAndView
(
View
view,
String
modelName,
Object
modelObject)
Convenient constructor to take a single model object. |
Method Summary | |
---|---|
ModelAndView
|
addAllObjects
(
Map
modelMap)
Add all entries contained in the provided map to the model. |
ModelAndView
|
addObject
(
String
modelName,
Object
modelObject)
Add an object to the model. |
void
|
clear
()
Clear the state of this ModelAndView object. |
Map
|
getModel
()
Return the model map. |
protected
Map
|
getModelInternal
()
Return the model map. |
View
|
getView
()
Return the View object, or
null
if we are using a view name to be resolved by the DispatcherServlet via a ViewResolver.
|
String
|
getViewName
()
Return the view name to be resolved by the DispatcherServlet via a ViewResolver, or
null
if we are using a View object.
|
boolean
|
isEmpty
()
Return whether this ModelAndView object is empty, i.e. whether it does not hold any view and does not contain a model. |
boolean
|
isReference
()
Return whether we use a view reference, i.e. true if the view has been specified via a name to be resolved by the DispatcherServlet via a ViewResolver. |
void
|
setView
(
View
view)
Set a View object for this ModelAndView. |
void
|
setViewName
(
String
viewName)
Set a view name for this ModelAndView, to be resolved by the DispatcherServlet via a ViewResolver. |
String
|
toString
()
Return diagnostic information about this model and view. |
收集自其它网页:
ModelAndView 类别就如其名称所看到的,是代表了Spring Web MVC程式中呈现画面时所使用Model资料物件与View资料物件,因为Java程式中一次仅仅能返回一个物件,所以ModelAndView的作用封装这两个物件,以方便您一次返回Model与View这两个物件。
最简单的ModelAndView是持有View的名称返回,之后View名称被View resolver,也就是实作org.springframework.web.servlet.View介面的实例解析,比如 InternalResourceView或JstlView等等,最简单的ModelAndView建构方式例如以下:
假设您要返回呈现画面时所需的Model资料物件,则能够使用Map物件来收集呈现资料时所需的资料,然后在建构ModelAndView作为建构时的引数,您能够使用以下这个版本号的ModelAndView建构方法:
Map物件中设定好键(Key)与值(Value),之后能够在要呈现的画面中取出加以显示(比如在JSP网页中),假设您要返回一个Model资料物件并指定Model的名称,则能够使用以下这个ModelAndView版本号:
藉由modelName,您能够在要View的实作类别中取出Model资料物件,并依据View所使用的技术来加以显示(可能是JSP网页、Pdf等呈现技术)。
ModelAndView类别也提供实作View介面的物件来作为建构时的引数,下面是三个可用的建构方法版本号:
ModelAndView(View view, Map model)
ModelAndView(View view, String modelName, Object modelObject)
一个实作View的实例例是org.springframework.web.servlet.view.RedirectView, ModelAndView预设是使用转发(Forward)方式来完毕请求的结果画面,使用RedirectView的话,则会使用又一次导向(Redirect)将请求重导至指定的结果画面位置,以呈现请求的结果,比如:
public ModelAndView handleRequest(....) ... {
...
return new ModelAndView(
new RedirectView(this.getViewPage()));
}
...
在这边,viewPage所设定的位址要是从伺服器网页根文件夹開始指定,而不是Web应用程式的根文件夹,所以您的getViewPage()传回的位址必须像是/FirstSpringMVC/pages/index.htm这种位址,当中FirstSpringMVC是您的Web应用程式文件夹。
使用转发(Forward)方式的话,网址列上并不会出现被转发的目标位址,并且转发方式是在Web应用程式之内进行,能够訪问Web应用程式所设定的内部文件夹,像是WEB-INF文件夹,因而您能够将一些要控管存取的资源放到WEB-INF下,如此使用者就无法直接请求这些资源,而必须透过 DispatcherServlet与Controller的处理与控管,才干够取得这些资源,转发方式仅仅能在Web应用程式中进行,不能指定至其他的 Web应用程式位址。
使用又一次导向(Redirect)的话,Web应用程式会要求client浏览器又一次发出一个所指定的请求位址,也就是实际上相当于client又一次连接至一个所指定的位址,因此浏览器的位址列上会出现被又一次导向的资讯,又一次导向的请求是由浏览器发出,所以不能訪问Web应用程式中的隐藏文件夹,像是WEB-INF,又一次导向是又一次要求一个网页,所以能够指定至其他的Web应用程式位址。
DispatcherServlet会依据传回的ModelAndView来解析View名称,并处理给予的Model。
View名称的解析是托付给实作org.springframework.web.servlet.ViewResolver介面的实例,ViewResolver介面定义例如以下:
public interface ViewResolver { public view resolveViewName( String , Locale locale) throws ServletException; }
ViewResolver的一个实例是InternalResourceViewResolver,名称解析完之后,实际的View绘制与Model转换处理是交给实作org.springframework.web.servlet.View的实例,View介面例如以下:
public interface View { public void render(Map model, HttpServletResquest resquest, HttpServletResponse response) throws ServletException, IOException; }
View的实作之前用过org.springframework.web.servlet.view.InternalResourceView,另外也还有JstlView、TilesView、VelocityView等等的实作,分别进行不同的表现展处理。