Improve @SessionAttributes support during redirect
Before this change attributes listed with @SessionAttributes would not be saved in the session when there was a redirect and the controller method declared a parameter of type RedirectAttributes. This change ensures it's the "default" model that is always the one checked for @SessionAttributes under all circumstances since RedirectAttributes is really only meant to provide String values to insert into or append to the the redirect URL. Issue: SPR-12542
This commit is contained in:
@@ -215,6 +215,32 @@ public class ModelFactoryTests {
|
||||
assertNull(this.sessionAttributeStore.retrieveAttribute(this.webRequest, attributeName));
|
||||
}
|
||||
|
||||
// SPR-12542
|
||||
|
||||
@Test
|
||||
public void updateModelWhenRedirecting() throws Exception {
|
||||
String attributeName = "sessionAttr";
|
||||
String attribute = "value";
|
||||
ModelAndViewContainer container = new ModelAndViewContainer();
|
||||
container.addAttribute(attributeName, attribute);
|
||||
|
||||
String queryParam = "123";
|
||||
String queryParamName = "q";
|
||||
container.setRedirectModel(new ModelMap(queryParamName, queryParam));
|
||||
container.setRedirectModelScenario(true);
|
||||
|
||||
WebDataBinder dataBinder = new WebDataBinder(attribute, attributeName);
|
||||
WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
|
||||
given(binderFactory.createBinder(this.webRequest, attribute, attributeName)).willReturn(dataBinder);
|
||||
|
||||
ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.sessionAttrsHandler);
|
||||
modelFactory.updateModel(this.webRequest, container);
|
||||
|
||||
assertEquals(queryParam, container.getModel().get(queryParamName));
|
||||
assertEquals(1, container.getModel().size());
|
||||
assertEquals(attribute, this.sessionAttributeStore.retrieveAttribute(this.webRequest, attributeName));
|
||||
}
|
||||
|
||||
|
||||
private String bindingResultKey(String key) {
|
||||
return BindingResult.MODEL_KEY_PREFIX + key;
|
||||
|
||||
Reference in New Issue
Block a user