KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > ModelAndView


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.servlet;
18
19 import java.util.Map JavaDoc;
20
21 import org.springframework.ui.ModelMap;
22
23 /**
24  * Holder for both Model and View in the web MVC framework.
25  * Note that these are entirely distinct. This class merely holds
26  * both to make it possible for a controller to return both model
27  * and view in a single return value.
28  *
29  * <p>Represents a model and view returned by a handler, to be resolved
30  * by a DispatcherServlet. The view can take the form of a String
31  * view name which will need to be resolved by a ViewResolver object;
32  * alternatively a View object can be specified directly. The model
33  * is a Map, allowing the use of multiple objects keyed by name.
34  *
35  * @author Rod Johnson
36  * @author Juergen Hoeller
37  * @author Rob Harrop
38  * @see DispatcherServlet
39  * @see ViewResolver
40  * @see HandlerAdapter#handle
41  * @see org.springframework.web.servlet.mvc.Controller#handleRequest
42  */

43 public class ModelAndView {
44
45     /** View instance or view name String */
46     private Object JavaDoc view;
47
48     /** Model Map */
49     private ModelMap model;
50
51     /**
52      * Indicates whether or not this instance has been cleared with a call to {@link #clear()}.
53      */

54     private boolean cleared;
55
56
57     /**
58      * Default constructor for bean-style usage: populating bean
59      * properties instead of passing in constructor arguments.
60      * @see #setView(View)
61      * @see #setViewName(String)
62      */

63     public ModelAndView() {
64     }
65
66     /**
67      * Convenient constructor when there is no model data to expose.
68      * Can also be used in conjunction with <code>addObject</code>.
69      * @param viewName name of the View to render, to be resolved
70      * by the DispatcherServlet's ViewResolver
71      * @see #addObject
72      */

73     public ModelAndView(String JavaDoc viewName) {
74         this.view = viewName;
75     }
76
77     /**
78      * Convenient constructor when there is no model data to expose.
79      * Can also be used in conjunction with <code>addObject</code>.
80      * @param view View object to render
81      * @see #addObject
82      */

83     public ModelAndView(View view) {
84         this.view = view;
85     }
86
87     /**
88      * Creates new ModelAndView given a view name and a model.
89      * @param viewName name of the View to render, to be resolved
90      * by the DispatcherServlet's ViewResolver
91      * @param model Map of model names (Strings) to model objects
92      * (Objects). Model entries may not be <code>null</code>, but the
93      * model Map may be <code>null</code> if there is no model data.
94      */

95     public ModelAndView(String JavaDoc viewName, Map JavaDoc model) {
96         this.view = viewName;
97         if (model != null) {
98             getModelMap().addAllObjects(model);
99         }
100     }
101
102     /**
103      * Creates new ModelAndView given a View object and a model.
104      * <emphasis>Note: the supplied model data is copied into the internal
105      * storage of this class. You should not consider to modify the supplied
106      * Map after supplying it to this class</emphasis>
107      * @param view View object to render
108      * @param model Map of model names (Strings) to model objects
109      * (Objects). Model entries may not be <code>null</code>, but the
110      * model Map may be <code>null</code> if there is no model data.
111      */

112     public ModelAndView(View view, Map JavaDoc model) {
113         this.view = view;
114         if (model != null) {
115             getModelMap().addAllObjects(model);
116         }
117     }
118
119     /**
120      * Convenient constructor to take a single model object.
121      * @param viewName name of the View to render, to be resolved
122      * by the DispatcherServlet's ViewResolver
123      * @param modelName name of the single entry in the model
124      * @param modelObject the single model object
125      */

126     public ModelAndView(String JavaDoc viewName, String JavaDoc modelName, Object JavaDoc modelObject) {
127         this.view = viewName;
128         addObject(modelName, modelObject);
129     }
130
131     /**
132      * Convenient constructor to take a single model object.
133      * @param view View object to render
134      * @param modelName name of the single entry in the model
135      * @param modelObject the single model object
136      */

137     public ModelAndView(View view, String JavaDoc modelName, Object JavaDoc modelObject) {
138         this.view = view;
139         addObject(modelName, modelObject);
140     }
141
142
143     /**
144      * Set a view name for this ModelAndView, to be resolved by the
145      * DispatcherServlet via a ViewResolver. Will override any
146      * pre-existing view name or View.
147      */

148     public void setViewName(String JavaDoc viewName) {
149         this.view = viewName;
150     }
151
152     /**
153      * Return the view name to be resolved by the DispatcherServlet
154      * via a ViewResolver, or <code>null</code> if we are using a View object.
155      */

156     public String JavaDoc getViewName() {
157         return (this.view instanceof String JavaDoc ? (String JavaDoc) this.view : null);
158     }
159
160     /**
161      * Set a View object for this ModelAndView. Will override any
162      * pre-existing view name or View.
163      */

164     public void setView(View view) {
165         this.view = view;
166     }
167
168     /**
169      * Return the View object, or <code>null</code> if we are using a view name
170      * to be resolved by the DispatcherServlet via a ViewResolver.
171      */

172     public View getView() {
173         return (this.view instanceof View ? (View) this.view : null);
174     }
175
176     /**
177      * Indicate whether or not this <code>ModelAndView</code> has a view, either
178      * as a view name or as a direct {@link View} instance.
179      */

180     public boolean hasView() {
181         return (this.view != null);
182     }
183
184     /**
185      * Return whether we use a view reference, i.e. <code>true</code>
186      * if the view has been specified via a name to be resolved by the
187      * DispatcherServlet via a ViewResolver.
188      */

189     public boolean isReference() {
190         return (this.view instanceof String JavaDoc);
191     }
192
193     /**
194      * Return the model map. May return null.
195      * Called by DispatcherServlet for evaluation of the model.
196      */

197     protected Map JavaDoc getModelInternal() {
198         return this.model;
199     }
200
201     /**
202      * Return the underlying <code>ModelMap</code> instance (never <code>null</code>).
203      */

204     public ModelMap getModelMap() {
205         if (this.model == null) {
206             this.model = new ModelMap();
207         }
208         return this.model;
209     }
210
211     /**
212      * Return the model map. Never returns <code>null</code>.
213      * To be called by application code for modifying the model.
214      */

215     public Map JavaDoc getModel() {
216         return getModelMap();
217     }
218
219
220     /**
221      * Add an object to the model using parameter name generation.
222      * @param modelObject the object to add to the model (never <code>null</code>)
223      * @see ModelMap#addObject(Object)
224      */

225     public ModelAndView addObject(Object JavaDoc modelObject) {
226         getModelMap().addObject(modelObject);
227         return this;
228     }
229
230     /**
231      * Add an object to the model.
232      * @param modelName name of the object to add to the model (never <code>null</code>)
233      * @param modelObject object to add to the model (can be <code>null</code>)
234      * @return this ModelAndView, convenient to allow usages like
235      * return modelAndView.addObject("foo", bar);
236      */

237     public ModelAndView addObject(String JavaDoc modelName, Object JavaDoc modelObject) {
238         getModelMap().addObject(modelName, modelObject);
239         return this;
240     }
241
242     /**
243      * Add all entries contained in the provided map to the model.
244      * @param modelMap a map of modelName -> modelObject pairs
245      * @return this ModelAndView, convenient to allow usages like
246      * return modelAndView.addAllObjects(myModelMap);
247      */

248     public ModelAndView addAllObjects(Map JavaDoc modelMap) {
249         getModelMap().addAllObjects(modelMap);
250         return this;
251     }
252
253
254     /**
255      * Clear the state of this ModelAndView object.
256      * The object will be empty afterwards.
257      * <p>Can be used to suppress rendering of a given ModelAndView object
258      * in the <code>postHandle</code> method of a HandlerInterceptor.
259      * @see #isEmpty()
260      * @see HandlerInterceptor#postHandle
261      */

262     public void clear() {
263         this.view = null;
264         this.model = null;
265         this.cleared = true;
266     }
267
268     /**
269      * Return whether this ModelAndView object is empty
270      * i.e. whether it does not hold any view and does not contain a model.
271      */

272     public boolean isEmpty() {
273         return (this.view == null && this.model == null);
274     }
275
276     /**
277      * Return whether this ModelAndView object is empty as a result of a call to {@link #clear}
278      * i.e. whether it does not hold any view and does not contain a model.
279      * Returns <code>false</code> if any additional state was added to the instance
280      * <strong>after</strong> the call to {@link #clear}.
281      * @see #clear()
282      */

283     public boolean wasCleared() {
284         return (this.cleared && isEmpty());
285     }
286
287
288     /**
289      * Return diagnostic information about this model and view.
290      */

291     public String JavaDoc toString() {
292         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("ModelAndView: ");
293         if (isReference()) {
294             buf.append("reference to view with name '").append(this.view).append("'");
295         }
296         else {
297             buf.append("materialized View is [").append(this.view).append(']');
298         }
299         buf.append("; model is ").append(this.model);
300         return buf.toString();
301     }
302 }
303
Popular Tags