factored out getRequestDispatcher template method (SPR-7118)

This commit is contained in:
Juergen Hoeller 2010-04-22 21:46:56 +00:00
parent 8df0403e99
commit 101a5cd8ff
1 changed files with 30 additions and 17 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -214,10 +214,10 @@ public class InternalResourceView extends AbstractUrlBasedView {
String dispatcherPath = prepareForRendering(requestToExpose, response); String dispatcherPath = prepareForRendering(requestToExpose, response);
// Obtain a RequestDispatcher for the target resource (typically a JSP). // Obtain a RequestDispatcher for the target resource (typically a JSP).
RequestDispatcher rd = requestToExpose.getRequestDispatcher(dispatcherPath); RequestDispatcher rd = getRequestDispatcher(requestToExpose, dispatcherPath);
if (rd == null) { if (rd == null) {
throw new ServletException( throw new ServletException("Could not get RequestDispatcher for [" + getUrl() +
"Could not get RequestDispatcher for [" + getUrl() + "]: check that this file exists within your WAR"); "]: Check that the corresponding file exists within your web application archive!");
} }
// If already included or response already committed, perform include, else forward. // If already included or response already committed, perform include, else forward.
@ -239,6 +239,23 @@ public class InternalResourceView extends AbstractUrlBasedView {
} }
} }
/**
* Get the request handle to expose to the RequestDispatcher, i.e. to the view.
* <p>The default implementation wraps the original request for exposure of
* Spring beans as request attributes (if demanded).
* @param originalRequest the original servlet request as provided by the engine
* @return the wrapped request, or the original request if no wrapping is necessary
* @see #setExposeContextBeansAsAttributes
* @see org.springframework.web.context.support.ContextExposingHttpServletRequest
*/
protected HttpServletRequest getRequestToExpose(HttpServletRequest originalRequest) {
if (this.exposeContextBeansAsAttributes || this.exposedContextBeanNames != null) {
return new ContextExposingHttpServletRequest(
originalRequest, getWebApplicationContext(), this.exposedContextBeanNames);
}
return originalRequest;
}
/** /**
* Expose helpers unique to each rendering operation. This is necessary so that * Expose helpers unique to each rendering operation. This is necessary so that
* different rendering operations can't overwrite each other's contexts etc. * different rendering operations can't overwrite each other's contexts etc.
@ -281,20 +298,16 @@ public class InternalResourceView extends AbstractUrlBasedView {
} }
/** /**
* Get the request handle to expose to the RequestDispatcher, i.e. to the view. * Obtain the RequestDispatcher to use for the forward/include.
* <p>The default implementation wraps the original request for exposure of * <p>The default implementation simply calls
* Spring beans as request attributes (if demanded). * {@link HttpServletRequest#getRequestDispatcher(String)}.
* @param originalRequest the original servlet request as provided by the engine * Can be overridden in subclasses.
* @return the wrapped request, or the original request if no wrapping is necessary * @param request current HTTP request
* @see #setExposeContextBeansAsAttributes * @param path the target URL (as returned from {@link #prepareForRendering})
* @see org.springframework.web.context.support.ContextExposingHttpServletRequest * @return a corresponding RequestDispatcher
*/ */
protected HttpServletRequest getRequestToExpose(HttpServletRequest originalRequest) { protected RequestDispatcher getRequestDispatcher(HttpServletRequest request, String path) {
if (this.exposeContextBeansAsAttributes || this.exposedContextBeanNames != null) { return request.getRequestDispatcher(path);
return new ContextExposingHttpServletRequest(
originalRequest, getWebApplicationContext(), this.exposedContextBeanNames);
}
return originalRequest;
} }
/** /**