KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > InternalResourceView


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.view;
18
19 import java.util.Map JavaDoc;
20
21 import javax.servlet.RequestDispatcher JavaDoc;
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25
26 import org.springframework.web.util.WebUtils;
27
28 /**
29  * Wrapper for a JSP or other resource within the same web application.
30  * Exposes model objects as request attributes and forwards the request to
31  * the specified resource URL using a RequestDispatcher.
32  *
33  * <p>A URL for this view is supposed to specify a resource within the web
34  * application, suitable for RequestDispatcher's <code>forward</code> or
35  * <code>include</code> method.
36  *
37  * <p>If operating within an already included request or within a response that
38  * has already been committed, this view will fall back to an include instead of
39  * a forward. This can be enforced by calling <code>response.flushBuffer()</code>
40  * (which will commit the response) before rendering the view.
41  *
42  * <p>Typical usage with InternalResourceViewResolver would look as follows,
43  * from the perspective of the DispatcherServlet context definition:
44  *
45  * <pre class="code">&lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
46  * &lt;property name="prefix" value="/WEB-INF/jsp/"/&gt;
47  * &lt;property name="suffix" value=".jsp"/&gt;
48  * &lt;/bean&gt;</pre>
49  *
50  * Every view name returned from a handler will be translated to a JSP
51  * resource (for example: "myView" -> "/WEB-INF/jsp/myView.jsp"), using
52  * this view class by default.
53  *
54  * @author Rod Johnson
55  * @author Juergen Hoeller
56  * @author Rob Harrop
57  * @see javax.servlet.RequestDispatcher#forward
58  * @see javax.servlet.RequestDispatcher#include
59  * @see javax.servlet.ServletResponse#flushBuffer
60  * @see InternalResourceViewResolver
61  */

62 public class InternalResourceView extends AbstractUrlBasedView {
63
64     private boolean alwaysInclude = false;
65
66
67     /**
68      * Constructor for use as a bean.
69      * @see #setUrl
70      * @see #setAlwaysInclude
71      */

72     public InternalResourceView() {
73     }
74
75     /**
76      * Create a new InternalResourceView with the given URL.
77      * @param url the URL to forward to
78      * @see #setAlwaysInclude
79      */

80     public InternalResourceView(String JavaDoc url) {
81         super(url);
82     }
83
84     /**
85      * Create a new InternalResourceView with the given URL.
86      * @param url the URL to forward to
87      * @param alwaysInclude whether to always include the view rather than forward to it
88      */

89     public InternalResourceView(String JavaDoc url, boolean alwaysInclude) {
90         super(url);
91         this.alwaysInclude = alwaysInclude;
92     }
93
94
95     /**
96      * Specify whether to always include the view rather than forward to it.
97      * <p>Default is "false". Switch this flag on to enforce the use of a
98      * Servlet include, even if a forward would be possible.
99      * @see javax.servlet.RequestDispatcher#forward
100      * @see javax.servlet.RequestDispatcher#include
101      * @see #useInclude(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
102      */

103     public void setAlwaysInclude(boolean alwaysInclude) {
104         this.alwaysInclude = alwaysInclude;
105     }
106
107
108     /**
109      * Render the internal resource given the specified model.
110      * This includes setting the model as request attributes.
111      */

112     protected void renderMergedOutputModel(
113             Map JavaDoc model, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
114
115         // Expose the model object as request attributes.
116
exposeModelAsRequestAttributes(model, request);
117
118         // Expose helpers as request attributes, if any.
119
exposeHelpers(request);
120
121         // Determine the path for the request dispatcher.
122
String JavaDoc dispatcherPath = prepareForRendering(request, response);
123
124         // Forward to the resource (typically a JSP).
125
// Note: The JSP is supposed to determine the content type itself.
126
RequestDispatcher JavaDoc rd = request.getRequestDispatcher(dispatcherPath);
127         if (rd == null) {
128             throw new ServletException JavaDoc(
129                     "Could not get RequestDispatcher for [" + getUrl() + "]: check that this file exists within your WAR");
130         }
131
132         // If already included or response already committed, perform include, else forward.
133
if (useInclude(request, response)) {
134             rd.include(request, response);
135             if (logger.isDebugEnabled()) {
136                 logger.debug("Included resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'");
137             }
138         }
139
140         else {
141             exposeForwardRequestAttributes(request);
142             rd.forward(request, response);
143             if (logger.isDebugEnabled()) {
144                 logger.debug("Forwarded to resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'");
145             }
146         }
147     }
148
149     /**
150      * Expose helpers unique to each rendering operation. This is necessary so that
151      * different rendering operations can't overwrite each other's contexts etc.
152      * <p>Called by {@link #renderMergedOutputModel(Map, HttpServletRequest, HttpServletResponse)}.
153      * The default implementation is empty. This method can be overridden to add
154      * custom helpers as request attributes.
155      * @param request current HTTP request
156      * @throws Exception if there's a fatal error while we're adding attributes
157      * @see #renderMergedOutputModel
158      * @see JstlView#exposeHelpers
159      * @see org.springframework.web.servlet.view.tiles.TilesJstlView#exposeHelpers
160      */

161     protected void exposeHelpers(HttpServletRequest JavaDoc request) throws Exception JavaDoc {
162     }
163
164     /**
165      * Prepare for rendering, and determine the request dispatcher path
166      * to forward to (or to include).
167      * <p>This implementation simply returns the configured URL.
168      * Subclasses can override this to determine a resource to render,
169      * typically interpreting the URL in a different manner.
170      * @param request current HTTP request
171      * @param response current HTTP response
172      * @return the request dispatcher path to use
173      * @throws Exception if preparations failed
174      * @see #getUrl
175      * @see org.springframework.web.servlet.view.tiles.TilesView#prepareForRendering
176      */

177     protected String JavaDoc prepareForRendering(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
178             throws Exception JavaDoc {
179
180         return getUrl();
181     }
182
183     /**
184      * Determine whether to use RequestDispatcher's <code>include</code> or
185      * <code>forward</code> method.
186      * <p>Performs a check whether an include URI attribute is found in the request,
187      * indicating an include request, and whether the response has already been committed.
188      * In both cases, an include will be performed, as a forward is not possible anymore.
189      * @param request current HTTP request
190      * @param response current HTTP response
191      * @return <code>true</code> for include, <code>false</code> for forward
192      * @see javax.servlet.RequestDispatcher#forward
193      * @see javax.servlet.RequestDispatcher#include
194      * @see javax.servlet.ServletResponse#isCommitted
195      * @see org.springframework.web.util.WebUtils#isIncludeRequest
196      */

197     protected boolean useInclude(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
198         return (this.alwaysInclude || WebUtils.isIncludeRequest(request) || response.isCommitted());
199     }
200
201     /**
202      * Expose the current request URI and paths as {@link HttpServletRequest}
203      * attributes under the keys defined in the Servlet 2.4 specification,
204      * for Servlet 2.3- containers.
205      * <p>Does not override values if already present, to not conflict
206      * with Servlet 2.4+ containers.
207      * @see org.springframework.web.util.WebUtils#exposeForwardRequestAttributes
208      */

209     protected void exposeForwardRequestAttributes(HttpServletRequest JavaDoc request) {
210         WebUtils.exposeForwardRequestAttributes(request);
211     }
212
213 }
214
Popular Tags