Skip to content

Commit d7062f6

Browse files
committed
Ensure RedirectModel is initialized
This commit fixes an old bug in ModelAndViewContainer where getModel returns a new ModelMap instance that isn't saved and re-used. Issue: SPR-14045
1 parent bd82acb commit d7062f6

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

spring-web/src/main/java/org/springframework/web/method/support/ModelAndViewContainer.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ public ModelMap getModel() {
126126
return this.defaultModel;
127127
}
128128
else {
129-
return (this.redirectModel != null) ? this.redirectModel : new ModelMap();
129+
if (this.redirectModel == null) {
130+
this.redirectModel = new ModelMap();
131+
}
132+
return this.redirectModel;
130133
}
131134
}
132135

spring-web/src/test/java/org/springframework/web/method/support/ModelAndViewContainerTests.java

Lines changed: 12 additions & 1 deletion
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-2016 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.
@@ -76,4 +76,15 @@ public void ignoreDefaultModel() {
7676
assertTrue(this.mavContainer.getModel().isEmpty());
7777
}
7878

79+
@Test // SPR-14045
80+
public void ignoreDefaultModelAndWithoutRedirectModel() {
81+
this.mavContainer.setIgnoreDefaultModelOnRedirect(true);
82+
this.mavContainer.setRedirectModelScenario(true);
83+
this.mavContainer.addAttribute("name", "value");
84+
85+
assertEquals(1, this.mavContainer.getModel().size());
86+
assertEquals("value", this.mavContainer.getModel().get("name"));
87+
}
88+
89+
7990
}

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ModelAndViewMethodReturnValueHandlerTests.java

Lines changed: 15 additions & 1 deletion
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-2016 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.
@@ -143,6 +143,20 @@ public void handleRedirectAttributesWithoutRedirect() throws Exception {
143143
assertNotSame("RedirectAttributes should not be used if controller doesn't redirect", redirectAttributes, model);
144144
}
145145

146+
@Test // SPR-14045
147+
public void handleRedirectWithIgnoreDefaultModel() throws Exception {
148+
mavContainer.setIgnoreDefaultModelOnRedirect(true);
149+
150+
RedirectView redirectView = new RedirectView();
151+
ModelAndView mav = new ModelAndView(redirectView, "name", "value");
152+
handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest);
153+
154+
ModelMap model = mavContainer.getModel();
155+
assertSame(redirectView, mavContainer.getView());
156+
assertEquals(1, model.size());
157+
assertEquals("value", model.get("name"));
158+
}
159+
146160

147161
private MethodParameter getReturnValueParam(String methodName) throws Exception {
148162
Method method = getClass().getDeclaredMethod(methodName);

0 commit comments

Comments
 (0)