KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > portlet > 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.portlet;
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 DispatcherPortlet. 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 Juergen Hoeller
36  * @since 2.0
37  * @see org.springframework.web.portlet.DispatcherPortlet
38  * @see org.springframework.web.servlet.ViewResolver
39  * @see org.springframework.web.portlet.HandlerAdapter
40  * @see org.springframework.web.portlet.mvc.Controller
41  */

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

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

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

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

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

94     public ModelAndView(String JavaDoc viewName, Map JavaDoc model) {
95         this.view = viewName;
96         if (model != null) {
97             getModelMap().addAllObjects(model);
98         }
99     }
100
101     /**
102      * Create a new ModelAndView given a View object and a model.
103      * @param view View object to render (usually a Servlet MVC View object)
104      * @param model Map of model names (Strings) to model objects
105      * (Objects). Model entries may not be <code>null</code>, but the
106      * model Map may be <code>null</code> if there is no model data.
107      */

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

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

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

144     public void setViewName(String JavaDoc viewName) {
145         this.view = viewName;
146     }
147
148     /**
149      * Return the view name to be resolved by the DispatcherPortlet
150      * via a ViewResolver, or <code>null</code> if we are using a view object.
151      */

152     public String JavaDoc getViewName() {
153         return (this.view instanceof String JavaDoc ? (String JavaDoc) this.view : null);
154     }
155
156     /**
157      * Set a View object for this ModelAndView. Will override any
158      * pre-existing view name or View.
159      * <p>The given View object will usually be a Servlet MVC View object.
160      * This is nevertheless typed as Object to avoid a Servlet API dependency
161      * in the Portlet ModelAndView class.
162      */

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

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

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

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

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

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

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

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

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

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

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

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

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

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