Skip to content

Commit 4314da9

Browse files
committed
RedirectView and RequestContext use RequestContextUtils.findWebApplicationContext
Issue: SPR-13346
1 parent 7d30017 commit 4314da9

File tree

6 files changed

+100
-37
lines changed

6 files changed

+100
-37
lines changed

spring-web/src/main/java/org/springframework/web/context/support/WebApplicationObjectSupport.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -125,7 +125,11 @@ protected final ServletContext getServletContext() throws IllegalStateException
125125
if (this.servletContext != null) {
126126
return this.servletContext;
127127
}
128-
ServletContext servletContext = getWebApplicationContext().getServletContext();
128+
WebApplicationContext wac = getWebApplicationContext();
129+
if (wac == null) {
130+
return null;
131+
}
132+
ServletContext servletContext = wac.getServletContext();
129133
if (servletContext == null && isContextRequired()) {
130134
throw new IllegalStateException("WebApplicationObjectSupport instance [" + this +
131135
"] does not run within a ServletContext. Make sure the object is fully configured!");

spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public class DispatcherServlet extends FrameworkServlet {
206206
/**
207207
* Request attribute to hold the current web application context.
208208
* Otherwise only the global web app context is obtainable by tags etc.
209-
* @see org.springframework.web.servlet.support.RequestContextUtils#getWebApplicationContext
209+
* @see org.springframework.web.servlet.support.RequestContextUtils#findWebApplicationContext
210210
*/
211211
public static final String WEB_APPLICATION_CONTEXT_ATTRIBUTE = DispatcherServlet.class.getName() + ".CONTEXT";
212212

spring-webmvc/src/main/java/org/springframework/web/servlet/ViewRendererServlet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,7 +48,7 @@ public class ViewRendererServlet extends HttpServlet {
4848
/**
4949
* Request attribute to hold current web application context.
5050
* Otherwise only the global web app context is obtainable by tags etc.
51-
* @see org.springframework.web.servlet.support.RequestContextUtils#getWebApplicationContext
51+
* @see org.springframework.web.servlet.support.RequestContextUtils#findWebApplicationContext
5252
*/
5353
public static final String WEB_APPLICATION_CONTEXT_ATTRIBUTE = DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE;
5454

spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -89,12 +89,6 @@ public class RequestContext {
8989
*/
9090
public static final String WEB_APPLICATION_CONTEXT_ATTRIBUTE = RequestContext.class.getName() + ".CONTEXT";
9191

92-
/**
93-
* The name of the bean to use to look up in an implementation of
94-
* {@link RequestDataValueProcessor} has been configured.
95-
*/
96-
private static final String REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME = "requestDataValueProcessor";
97-
9892

9993
protected static final boolean jstlPresent = ClassUtils.isPresent("javax.servlet.jsp.jstl.core.Config",
10094
RequestContext.class.getClassLoader());
@@ -236,7 +230,11 @@ protected void initContext(HttpServletRequest request, HttpServletResponse respo
236230
// ServletContext needs to be specified to be able to fall back to the root context!
237231
this.webApplicationContext = (WebApplicationContext) request.getAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE);
238232
if (this.webApplicationContext == null) {
239-
this.webApplicationContext = RequestContextUtils.getWebApplicationContext(request, servletContext);
233+
this.webApplicationContext = RequestContextUtils.findWebApplicationContext(request, servletContext);
234+
if (this.webApplicationContext == null) {
235+
throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet " +
236+
"request and no ContextLoaderListener registered?");
237+
}
240238
}
241239

242240
// Determine locale to use for this RequestContext.
@@ -271,9 +269,9 @@ else if (localeResolver != null) {
271269

272270
this.urlPathHelper = new UrlPathHelper();
273271

274-
if (this.webApplicationContext.containsBean(REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
272+
if (this.webApplicationContext.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
275273
this.requestDataValueProcessor = this.webApplicationContext.getBean(
276-
REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
274+
RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
277275
}
278276
}
279277

spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContextUtils.java

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
import org.springframework.context.i18n.TimeZoneAwareLocaleContext;
2828
import org.springframework.ui.context.Theme;
2929
import org.springframework.ui.context.ThemeSource;
30+
import org.springframework.web.context.ContextLoader;
3031
import org.springframework.web.context.WebApplicationContext;
3132
import org.springframework.web.context.support.WebApplicationContextUtils;
3233
import org.springframework.web.servlet.DispatcherServlet;
@@ -51,16 +52,26 @@
5152
*/
5253
public abstract class RequestContextUtils {
5354

55+
/**
56+
* The name of the bean to use to look up in an implementation of
57+
* {@link RequestDataValueProcessor} has been configured.
58+
* @since 4.2.1
59+
*/
60+
public static final String REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME = "requestDataValueProcessor";
61+
62+
5463
/**
5564
* Look for the WebApplicationContext associated with the DispatcherServlet
5665
* that has initiated request processing.
5766
* @param request current HTTP request
5867
* @return the request-specific web application context
5968
* @throws IllegalStateException if no servlet-specific context has been found
69+
* @see #getWebApplicationContext(ServletRequest, ServletContext)
70+
* @deprecated as of Spring 4.2.1, in favor of
71+
* {@link #findWebApplicationContext(HttpServletRequest)}
6072
*/
61-
public static WebApplicationContext getWebApplicationContext(ServletRequest request)
62-
throws IllegalStateException {
63-
73+
@Deprecated
74+
public static WebApplicationContext getWebApplicationContext(ServletRequest request) throws IllegalStateException {
6475
return getWebApplicationContext(request, null);
6576
}
6677

@@ -76,7 +87,12 @@ public static WebApplicationContext getWebApplicationContext(ServletRequest requ
7687
* if no request-specific context has been found
7788
* @throws IllegalStateException if neither a servlet-specific nor a
7889
* global context has been found
90+
* @see DispatcherServlet#WEB_APPLICATION_CONTEXT_ATTRIBUTE
91+
* @see WebApplicationContextUtils#getRequiredWebApplicationContext(ServletContext)
92+
* @deprecated as of Spring 4.2.1, in favor of
93+
* {@link #findWebApplicationContext(HttpServletRequest, ServletContext)}
7994
*/
95+
@Deprecated
8096
public static WebApplicationContext getWebApplicationContext(
8197
ServletRequest request, ServletContext servletContext) throws IllegalStateException {
8298

@@ -91,6 +107,57 @@ public static WebApplicationContext getWebApplicationContext(
91107
return webApplicationContext;
92108
}
93109

110+
/**
111+
* Look for the WebApplicationContext associated with the DispatcherServlet
112+
* that has initiated request processing, and for the global context if none
113+
* was found associated with the current request. The global context will
114+
* be found via the ServletContext or via ContextLoader's current context.
115+
* <p>NOTE: This variant remains compatible with Servlet 2.5, explicitly
116+
* checking a given ServletContext instead of deriving it from the request.
117+
* @param request current HTTP request
118+
* @param servletContext current servlet context
119+
* @return the request-specific WebApplicationContext, or the global one
120+
* if no request-specific context has been found, or {@code null} if none
121+
* @since 4.2.1
122+
* @see DispatcherServlet#WEB_APPLICATION_CONTEXT_ATTRIBUTE
123+
* @see WebApplicationContextUtils#getWebApplicationContext(ServletContext)
124+
* @see ContextLoader#getCurrentWebApplicationContext()
125+
*/
126+
public static WebApplicationContext findWebApplicationContext(
127+
HttpServletRequest request, ServletContext servletContext) {
128+
129+
WebApplicationContext webApplicationContext = (WebApplicationContext) request.getAttribute(
130+
DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
131+
if (webApplicationContext == null) {
132+
if (servletContext != null) {
133+
webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
134+
}
135+
if (webApplicationContext == null) {
136+
webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
137+
}
138+
}
139+
return webApplicationContext;
140+
}
141+
142+
/**
143+
* Look for the WebApplicationContext associated with the DispatcherServlet
144+
* that has initiated request processing, and for the global context if none
145+
* was found associated with the current request. The global context will
146+
* be found via the ServletContext or via ContextLoader's current context.
147+
* <p>NOTE: This variant requires Servlet 3.0+ and is generally recommended
148+
* for forward-looking custom user code.
149+
* @param request current HTTP request
150+
* @return the request-specific WebApplicationContext, or the global one
151+
* if no request-specific context has been found, or {@code null} if none
152+
* @since 4.2.1
153+
* @see #findWebApplicationContext(HttpServletRequest, ServletContext)
154+
* @see ServletRequest#getServletContext()
155+
* @see ContextLoader#getCurrentWebApplicationContext()
156+
*/
157+
public static WebApplicationContext findWebApplicationContext(HttpServletRequest request) {
158+
return findWebApplicationContext(request, request.getServletContext());
159+
}
160+
94161
/**
95162
* Return the LocaleResolver that has been bound to the request by the
96163
* DispatcherServlet.

spring-webmvc/src/main/java/org/springframework/web/servlet/view/RedirectView.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,14 +36,12 @@
3636
import org.springframework.util.CollectionUtils;
3737
import org.springframework.util.ObjectUtils;
3838
import org.springframework.util.StringUtils;
39-
import org.springframework.web.context.ContextLoader;
4039
import org.springframework.web.context.WebApplicationContext;
4140
import org.springframework.web.servlet.FlashMap;
4241
import org.springframework.web.servlet.FlashMapManager;
4342
import org.springframework.web.servlet.HandlerMapping;
4443
import org.springframework.web.servlet.SmartView;
4544
import org.springframework.web.servlet.View;
46-
import org.springframework.web.servlet.support.RequestContext;
4745
import org.springframework.web.servlet.support.RequestContextUtils;
4846
import org.springframework.web.servlet.support.RequestDataValueProcessor;
4947
import org.springframework.web.util.UriComponents;
@@ -299,7 +297,7 @@ protected void renderMergedOutputModel(Map<String, Object> model, HttpServletReq
299297
}
300298

301299
/**
302-
* Creates the target URL by checking if the redirect string is a URI template first,
300+
* Create the target URL by checking if the redirect string is a URI template first,
303301
* expanding it with the given model, and then optionally appending simple type model
304302
* attributes as query String parameters.
305303
*/
@@ -554,27 +552,23 @@ protected String urlEncode(String input, String encodingScheme) throws Unsupport
554552
/**
555553
* Find the registered {@link RequestDataValueProcessor}, if any, and allow
556554
* it to update the redirect target URL.
555+
* @param targetUrl the given redirect URL
557556
* @return the updated URL or the same as URL as the one passed in
558557
*/
559558
protected String updateTargetUrl(String targetUrl, Map<String, Object> model,
560559
HttpServletRequest request, HttpServletResponse response) {
561560

562-
RequestContext requestContext = null;
563-
if (getWebApplicationContext() != null) {
564-
requestContext = createRequestContext(request, response, model);
561+
WebApplicationContext wac = getWebApplicationContext();
562+
if (wac == null) {
563+
wac = RequestContextUtils.findWebApplicationContext(request, getServletContext());
565564
}
566-
else {
567-
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
568-
if (wac != null && wac.getServletContext() != null) {
569-
requestContext = new RequestContext(request, response, wac.getServletContext(), model);
570-
}
571-
}
572-
if (requestContext != null) {
573-
RequestDataValueProcessor processor = requestContext.getRequestDataValueProcessor();
574-
if (processor != null) {
575-
targetUrl = processor.processUrl(request, targetUrl);
576-
}
565+
566+
if (wac != null && wac.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
567+
RequestDataValueProcessor processor = wac.getBean(
568+
RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
569+
return processor.processUrl(request, targetUrl);
577570
}
571+
578572
return targetUrl;
579573
}
580574

0 commit comments

Comments
 (0)