From f3c082abacbc9cbfc53030fa24509f319c7f2d9c Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Wed, 14 Sep 2022 18:13:38 +0100 Subject: [PATCH] Deprecate ignoreDefaultModelOnRedirect property Closes gh-28324 --- .../standalone/MultipartControllerTests.java | 141 ++++-------------- .../standalone/MultipartControllerTests.java | 119 +++++---------- .../method/support/ModelAndViewContainer.java | 9 +- .../support/ModelAndViewContainerTests.java | 6 +- .../RequestMappingHandlerAdapter.java | 11 +- ...tationDrivenBeanDefinitionParserTests.java | 3 +- .../web/servlet/config/MvcNamespaceTests.java | 3 +- ...MvcConfigurationSupportExtensionTests.java | 4 +- ...lAndViewMethodReturnValueHandlerTests.java | 2 - .../RequestMappingHandlerAdapterTests.java | 1 - 10 files changed, 82 insertions(+), 217 deletions(-) diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/MultipartControllerTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/MultipartControllerTests.java index 283c074fd84..d9206ea8197 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/MultipartControllerTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/MultipartControllerTests.java @@ -34,10 +34,8 @@ import org.junit.jupiter.api.Test; import org.springframework.http.MediaType; import org.springframework.http.client.MultipartBodyBuilder; import org.springframework.stereotype.Controller; -import org.springframework.test.web.reactive.server.EntityExchangeResult; import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.servlet.client.MockMvcWebTestClient; -import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -46,7 +44,6 @@ import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.multipart.MultipartFile; import static org.assertj.core.api.Assertions.assertThat; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; /** * {@link MockMvcWebTestClient} equivalent of the MockMvc @@ -69,28 +66,18 @@ public class MultipartControllerTests { bodyBuilder.part("file", fileContent).filename("orig"); bodyBuilder.part("json", json, MediaType.APPLICATION_JSON); - EntityExchangeResult exchangeResult = testClient.post().uri("/multipartfile") + testClient.post().uri("/multipartfile") .bodyValue(bodyBuilder.build()) .exchange() .expectStatus().isFound() .expectBody().isEmpty(); - // Further assertions on the server response - MockMvcWebTestClient.resultActionsFor(exchangeResult) - .andExpect(model().attribute("fileContent", fileContent)) - .andExpect(model().attribute("jsonContent", json)); - // Now try the same with HTTP PUT - exchangeResult = testClient.put().uri("/multipartfile-via-put") + testClient.put().uri("/multipartfile-via-put") .bodyValue(bodyBuilder.build()) .exchange() .expectStatus().isFound() .expectBody().isEmpty(); - - // Further assertions on the server response - MockMvcWebTestClient.resultActionsFor(exchangeResult) - .andExpect(model().attribute("fileContent", fileContent)) - .andExpect(model().attribute("jsonContent", json)); } @Test @@ -110,16 +97,11 @@ public class MultipartControllerTests { bodyBuilder.part("file", fileContent).filename("orig"); bodyBuilder.part("json", json, MediaType.APPLICATION_JSON); - EntityExchangeResult exchangeResult = testClient.post().uri("/multipartfilearray") + testClient.post().uri("/multipartfilearray") .bodyValue(bodyBuilder.build()) .exchange() .expectStatus().isFound() .expectBody().isEmpty(); - - // Further assertions on the server response - MockMvcWebTestClient.resultActionsFor(exchangeResult) - .andExpect(model().attribute("fileContent", fileContent)) - .andExpect(model().attribute("jsonContent", json)); } @Test @@ -138,16 +120,11 @@ public class MultipartControllerTests { bodyBuilder.part("file", fileContent).filename("orig"); bodyBuilder.part("json", json, MediaType.APPLICATION_JSON); - EntityExchangeResult exchangeResult = testClient.post().uri("/optionalfile") + testClient.post().uri("/optionalfile") .bodyValue(bodyBuilder.build()) .exchange() .expectStatus().isFound() .expectBody().isEmpty(); - - // Further assertions on the server response - MockMvcWebTestClient.resultActionsFor(exchangeResult) - .andExpect(model().attribute("fileContent", fileContent)) - .andExpect(model().attribute("jsonContent", json)); } @Test @@ -157,16 +134,11 @@ public class MultipartControllerTests { MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder(); bodyBuilder.part("json", json, MediaType.APPLICATION_JSON); - EntityExchangeResult exchangeResult = testClient.post().uri("/optionalfile") + testClient.post().uri("/optionalfile") .bodyValue(bodyBuilder.build()) .exchange() .expectStatus().isFound() .expectBody().isEmpty(); - - // Further assertions on the server response - MockMvcWebTestClient.resultActionsFor(exchangeResult) - .andExpect(model().attributeDoesNotExist("fileContent")) - .andExpect(model().attribute("jsonContent", json)); } @Test @@ -179,16 +151,11 @@ public class MultipartControllerTests { bodyBuilder.part("file", fileContent).filename("orig"); bodyBuilder.part("json", json, MediaType.APPLICATION_JSON); - EntityExchangeResult exchangeResult = testClient.post().uri("/optionalfilearray") + testClient.post().uri("/optionalfilearray") .bodyValue(bodyBuilder.build()) .exchange() .expectStatus().isFound() .expectBody().isEmpty(); - - // Further assertions on the server response - MockMvcWebTestClient.resultActionsFor(exchangeResult) - .andExpect(model().attribute("fileContent", fileContent)) - .andExpect(model().attribute("jsonContent", json)); } @Test @@ -198,16 +165,11 @@ public class MultipartControllerTests { MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder(); bodyBuilder.part("json", json, MediaType.APPLICATION_JSON); - EntityExchangeResult exchangeResult = testClient.post().uri("/optionalfilearray") + testClient.post().uri("/optionalfilearray") .bodyValue(bodyBuilder.build()) .exchange() .expectStatus().isFound() .expectBody().isEmpty(); - - // Further assertions on the server response - MockMvcWebTestClient.resultActionsFor(exchangeResult) - .andExpect(model().attributeDoesNotExist("fileContent")) - .andExpect(model().attribute("jsonContent", json)); } @Test @@ -220,16 +182,11 @@ public class MultipartControllerTests { bodyBuilder.part("file", fileContent).filename("orig"); bodyBuilder.part("json", json, MediaType.APPLICATION_JSON); - EntityExchangeResult exchangeResult = testClient.post().uri("/optionalfilelist") + testClient.post().uri("/optionalfilelist") .bodyValue(bodyBuilder.build()) .exchange() .expectStatus().isFound() .expectBody().isEmpty(); - - // Further assertions on the server response - MockMvcWebTestClient.resultActionsFor(exchangeResult) - .andExpect(model().attribute("fileContent", fileContent)) - .andExpect(model().attribute("jsonContent", json)); } @Test @@ -239,16 +196,11 @@ public class MultipartControllerTests { MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder(); bodyBuilder.part("json", json, MediaType.APPLICATION_JSON); - EntityExchangeResult exchangeResult = testClient.post().uri("/optionalfilelist") + testClient.post().uri("/optionalfilelist") .bodyValue(bodyBuilder.build()) .exchange() .expectStatus().isFound() .expectBody().isEmpty(); - - // Further assertions on the server response - MockMvcWebTestClient.resultActionsFor(exchangeResult) - .andExpect(model().attributeDoesNotExist("fileContent")) - .andExpect(model().attribute("jsonContent", json)); } @Test @@ -260,16 +212,11 @@ public class MultipartControllerTests { bodyBuilder.part("file", fileContent).filename("orig"); bodyBuilder.part("json", json, MediaType.APPLICATION_JSON); - EntityExchangeResult exchangeResult = testClient.post().uri("/multipartfile") + testClient.post().uri("/multipartfile") .bodyValue(bodyBuilder.build()) .exchange() .expectStatus().isFound() .expectBody().isEmpty(); - - // Further assertions on the server response - MockMvcWebTestClient.resultActionsFor(exchangeResult) - .andExpect(model().attribute("fileContent", fileContent)) - .andExpect(model().attribute("jsonContent", json)); } @Test @@ -283,15 +230,11 @@ public class MultipartControllerTests { .filter(new RequestWrappingFilter()) .build(); - EntityExchangeResult exchangeResult = client.post().uri("/multipartfile") + client.post().uri("/multipartfile") .bodyValue(bodyBuilder.build()) .exchange() .expectStatus().isFound() .expectBody().isEmpty(); - - // Further assertions on the server response - MockMvcWebTestClient.resultActionsFor(exchangeResult) - .andExpect(model().attribute("jsonContent", json)); } @@ -301,110 +244,78 @@ public class MultipartControllerTests { @PostMapping("/multipartfile") public String processMultipartFile(@RequestParam(required = false) MultipartFile file, - @RequestPart(required = false) Map json, Model model) throws IOException { - - if (file != null) { - model.addAttribute("fileContent", file.getBytes()); - } - if (json != null) { - model.addAttribute("jsonContent", json); - } + @RequestPart(required = false) Map json) { return "redirect:/index"; } @PutMapping("/multipartfile-via-put") public String processMultipartFileViaHttpPut(@RequestParam(required = false) MultipartFile file, - @RequestPart(required = false) Map json, Model model) throws IOException { + @RequestPart(required = false) Map json) { - return processMultipartFile(file, json, model); + return processMultipartFile(file, json); } @PostMapping("/multipartfilearray") public String processMultipartFileArray(@RequestParam(required = false) MultipartFile[] file, - @RequestPart(required = false) Map json, Model model) throws IOException { + @RequestPart(required = false) Map json) throws IOException { if (file != null && file.length > 0) { byte[] content = file[0].getBytes(); assertThat(file[1].getBytes()).isEqualTo(content); - model.addAttribute("fileContent", content); - } - if (json != null) { - model.addAttribute("jsonContent", json); } - return "redirect:/index"; } @PostMapping("/multipartfilelist") public String processMultipartFileList(@RequestParam(required = false) List file, - @RequestPart(required = false) Map json, Model model) throws IOException { + @RequestPart(required = false) Map json) throws IOException { if (file != null && !file.isEmpty()) { byte[] content = file.get(0).getBytes(); assertThat(file.get(1).getBytes()).isEqualTo(content); - model.addAttribute("fileContent", content); - } - if (json != null) { - model.addAttribute("jsonContent", json); } - return "redirect:/index"; } @PostMapping("/optionalfile") - public String processOptionalFile(@RequestParam Optional file, - @RequestPart Map json, Model model) throws IOException { - - if (file.isPresent()) { - model.addAttribute("fileContent", file.get().getBytes()); - } - model.addAttribute("jsonContent", json); + public String processOptionalFile( + @RequestParam Optional file, @RequestPart Map json) { return "redirect:/index"; } @PostMapping("/optionalfilearray") - public String processOptionalFileArray(@RequestParam Optional file, - @RequestPart Map json, Model model) throws IOException { + public String processOptionalFileArray( + @RequestParam Optional file, @RequestPart Map json) + throws IOException { if (file.isPresent()) { byte[] content = file.get()[0].getBytes(); assertThat(file.get()[1].getBytes()).isEqualTo(content); - model.addAttribute("fileContent", content); } - model.addAttribute("jsonContent", json); - return "redirect:/index"; } @PostMapping("/optionalfilelist") - public String processOptionalFileList(@RequestParam Optional> file, - @RequestPart Map json, Model model) throws IOException { + public String processOptionalFileList( + @RequestParam Optional> file, @RequestPart Map json) + throws IOException { if (file.isPresent()) { byte[] content = file.get().get(0).getBytes(); assertThat(file.get().get(1).getBytes()).isEqualTo(content); - model.addAttribute("fileContent", content); } - model.addAttribute("jsonContent", json); - return "redirect:/index"; } @PostMapping("/part") - public String processPart(@RequestParam Part part, - @RequestPart Map json, Model model) throws IOException { - - model.addAttribute("fileContent", part.getInputStream()); - model.addAttribute("jsonContent", json); - + public String processPart(@RequestParam Part part, @RequestPart Map json) { return "redirect:/index"; } @PostMapping("/json") - public String processMultipart(@RequestPart Map json, Model model) { - model.addAttribute("json", json); + public String processMultipart(@RequestPart Map json) { return "redirect:/index"; } } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/MultipartControllerTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/MultipartControllerTests.java index cf9de28ef67..497240a45d7 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/MultipartControllerTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/MultipartControllerTests.java @@ -40,8 +40,6 @@ import org.springframework.mock.web.MockPart; import org.springframework.stereotype.Controller; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder; -import org.springframework.ui.Model; -import org.springframework.util.StreamUtils; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; @@ -49,11 +47,11 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; @@ -88,9 +86,7 @@ class MultipartControllerTests { standaloneSetup(new MultipartController()).build() .perform(requestBuilder.file(jsonPart)) - .andExpect(status().isFound()) - .andExpect(model().attribute("fileContent", fileContent)) - .andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah"))); + .andExpect(status().isFound()); } @Test @@ -111,9 +107,7 @@ class MultipartControllerTests { standaloneSetup(new MultipartController()).build() .perform(multipart("/multipartfilearray").file(filePart1).file(filePart2).file(jsonPart)) - .andExpect(status().isFound()) - .andExpect(model().attribute("fileContent", fileContent)) - .andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah"))); + .andExpect(status().isFound()); } @Test @@ -141,9 +135,7 @@ class MultipartControllerTests { standaloneSetup(new MultipartController()).build() .perform(multipart("/multipartfilelist").file(filePart1).file(filePart2).file(jsonPart)) - .andExpect(status().isFound()) - .andExpect(model().attribute("fileContent", fileContent)) - .andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah"))); + .andExpect(status().isFound()); } @Test @@ -170,9 +162,7 @@ class MultipartControllerTests { standaloneSetup(new MultipartController()).build() .perform(multipart("/optionalfile").file(filePart).file(jsonPart)) - .andExpect(status().isFound()) - .andExpect(model().attribute("fileContent", fileContent)) - .andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah"))); + .andExpect(status().isFound()); } @Test @@ -182,9 +172,7 @@ class MultipartControllerTests { standaloneSetup(new MultipartController()).build() .perform(multipart("/optionalfile").file(jsonPart)) - .andExpect(status().isFound()) - .andExpect(model().attributeDoesNotExist("fileContent")) - .andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah"))); + .andExpect(status().isFound()); } @Test @@ -198,9 +186,7 @@ class MultipartControllerTests { standaloneSetup(new MultipartController()).build() .perform(multipart("/optionalfilearray").file(filePart1).file(filePart2).file(jsonPart)) - .andExpect(status().isFound()) - .andExpect(model().attribute("fileContent", fileContent)) - .andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah"))); + .andExpect(status().isFound()); } @Test @@ -210,9 +196,7 @@ class MultipartControllerTests { standaloneSetup(new MultipartController()).build() .perform(multipart("/optionalfilearray").file(jsonPart)) - .andExpect(status().isFound()) - .andExpect(model().attributeDoesNotExist("fileContent")) - .andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah"))); + .andExpect(status().isFound()); } @Test @@ -226,9 +210,7 @@ class MultipartControllerTests { standaloneSetup(new MultipartController()).build() .perform(multipart("/optionalfilelist").file(filePart1).file(filePart2).file(jsonPart)) - .andExpect(status().isFound()) - .andExpect(model().attribute("fileContent", fileContent)) - .andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah"))); + .andExpect(status().isFound()); } @Test @@ -238,9 +220,7 @@ class MultipartControllerTests { standaloneSetup(new MultipartController()).build() .perform(multipart("/optionalfilelist").file(jsonPart)) - .andExpect(status().isFound()) - .andExpect(model().attributeDoesNotExist("fileContent")) - .andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah"))); + .andExpect(status().isFound()); } @Test @@ -250,8 +230,7 @@ class MultipartControllerTests { standaloneSetup(new MultipartController()).build() .perform(multipart("/multipartfilebinding").part(filePart)) - .andExpect(status().isFound()) - .andExpect(model().attribute("fileContent", fileContent)); + .andExpect(status().isFound()); } @Test // SPR-13317 @@ -263,128 +242,96 @@ class MultipartControllerTests { MockMvc mockMvc = standaloneSetup(new MultipartController()).addFilter(filter).build(); Map jsonMap = Collections.singletonMap("name", "yeeeah"); - mockMvc.perform(multipart("/json").file(jsonPart)).andExpect(model().attribute("json", jsonMap)); + mockMvc.perform(multipart("/json").file(jsonPart)).andExpect(status().isFound()); } + @SuppressWarnings("OptionalUsedAsFieldOrParameterType") @Controller private static class MultipartController { @PostMapping("/multipartfile") public String processMultipartFile(@RequestParam(required = false) MultipartFile file, - @RequestPart(required = false) Map json, Model model) throws IOException { - - if (file != null) { - model.addAttribute("fileContent", file.getBytes()); - } - if (json != null) { - model.addAttribute("jsonContent", json); - } + @RequestPart(required = false) Map json) throws IOException { return "redirect:/index"; } @PutMapping("/multipartfile-via-put") public String processMultipartFileViaHttpPut(@RequestParam(required = false) MultipartFile file, - @RequestPart(required = false) Map json, Model model) throws IOException { + @RequestPart(required = false) Map json) throws IOException { - return processMultipartFile(file, json, model); + return processMultipartFile(file, json); } @PostMapping("/multipartfilearray") public String processMultipartFileArray(@RequestParam(required = false) MultipartFile[] file, - @RequestPart(required = false) Map json, Model model) throws IOException { + @RequestPart(required = false) Map json) throws IOException { if (file != null && file.length > 0) { byte[] content = file[0].getBytes(); assertThat(file[1].getBytes()).isEqualTo(content); - model.addAttribute("fileContent", content); } - if (json != null) { - model.addAttribute("jsonContent", json); - } - return "redirect:/index"; } @PostMapping("/multipartfilelist") public String processMultipartFileList(@RequestParam(required = false) List file, - @RequestPart(required = false) Map json, Model model) throws IOException { + @RequestPart(required = false) Map json) throws IOException { if (file != null && !file.isEmpty()) { byte[] content = file.get(0).getBytes(); assertThat(file.get(1).getBytes()).isEqualTo(content); - model.addAttribute("fileContent", content); - } - if (json != null) { - model.addAttribute("jsonContent", json); } - return "redirect:/index"; } @PostMapping("/optionalfile") - public String processOptionalFile(@RequestParam Optional file, - @RequestPart Map json, Model model) throws IOException { - - if (file.isPresent()) { - model.addAttribute("fileContent", file.get().getBytes()); - } - model.addAttribute("jsonContent", json); + public String processOptionalFile( + @RequestParam Optional file, @RequestPart Map json) { return "redirect:/index"; } @PostMapping("/optionalfilearray") - public String processOptionalFileArray(@RequestParam Optional file, - @RequestPart Map json, Model model) throws IOException { + public String processOptionalFileArray( + @RequestParam Optional file, @RequestPart Map json) + throws IOException { if (file.isPresent()) { byte[] content = file.get()[0].getBytes(); assertThat(file.get()[1].getBytes()).isEqualTo(content); - model.addAttribute("fileContent", content); } - model.addAttribute("jsonContent", json); - return "redirect:/index"; } @PostMapping("/optionalfilelist") - public String processOptionalFileList(@RequestParam Optional> file, - @RequestPart Map json, Model model) throws IOException { + public String processOptionalFileList( + @RequestParam Optional> file, @RequestPart Map json) + throws IOException { if (file.isPresent()) { byte[] content = file.get().get(0).getBytes(); assertThat(file.get().get(1).getBytes()).isEqualTo(content); - model.addAttribute("fileContent", content); } - model.addAttribute("jsonContent", json); return "redirect:/index"; } @PostMapping("/part") - public String processPart(@RequestPart Part part, - @RequestPart Map json, Model model) throws IOException { - - if (part != null) { - byte[] content = StreamUtils.copyToByteArray(part.getInputStream()); - model.addAttribute("fileContent", content); - } - model.addAttribute("jsonContent", json); - + public String processPart(@RequestPart Part part, @RequestPart Map json) { return "redirect:/index"; } @PostMapping("/json") - public String processMultipart(@RequestPart Map json, Model model) { - model.addAttribute("json", json); + public String processMultipart(@RequestPart Map json) { return "redirect:/index"; } @PostMapping("/multipartfilebinding") public String processMultipartFileBean( - MultipartFileBean multipartFileBean, Model model, BindingResult bindingResult) throws IOException { + MultipartFileBean multipartFileBean, RedirectAttributes model, BindingResult bindingResult) + throws IOException { if (!bindingResult.hasErrors()) { MultipartFile file = multipartFileBean.getFile(); @@ -396,6 +343,7 @@ class MultipartControllerTests { } } + private static class MultipartFileBean { private MultipartFile file; @@ -414,8 +362,9 @@ class MultipartControllerTests { private static class RequestWrappingFilter extends OncePerRequestFilter { @Override - protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, - FilterChain filterChain) throws IOException, ServletException { + protected void doFilterInternal( + HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) + throws IOException, ServletException { request = new HttpServletRequestWrapper(request); filterChain.doFilter(request, response); diff --git a/spring-web/src/main/java/org/springframework/web/method/support/ModelAndViewContainer.java b/spring-web/src/main/java/org/springframework/web/method/support/ModelAndViewContainer.java index c29a662b114..a9a3c94dd6c 100644 --- a/spring-web/src/main/java/org/springframework/web/method/support/ModelAndViewContainer.java +++ b/spring-web/src/main/java/org/springframework/web/method/support/ModelAndViewContainer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -49,7 +49,7 @@ import org.springframework.web.bind.support.SimpleSessionStatus; */ public class ModelAndViewContainer { - private boolean ignoreDefaultModelOnRedirect = false; + private boolean ignoreDefaultModelOnRedirect = true; @Nullable private Object view; @@ -83,8 +83,11 @@ public class ModelAndViewContainer { * is not declared. Setting it to {@code false} means the "default" model * may be used in a redirect if the controller method doesn't declare a * RedirectAttributes argument. - *

The default setting is {@code false}. + *

As of 6.0, this property is set to {@code true} by default. + * @deprecated as of 6.0 without a replacement; once removed, the default + * model will always be ignored on redirect */ + @Deprecated public void setIgnoreDefaultModelOnRedirect(boolean ignoreDefaultModelOnRedirect) { this.ignoreDefaultModelOnRedirect = ignoreDefaultModelOnRedirect; } diff --git a/spring-web/src/test/java/org/springframework/web/method/support/ModelAndViewContainerTests.java b/spring-web/src/test/java/org/springframework/web/method/support/ModelAndViewContainerTests.java index 13a2f1c5563..ca21a9a092a 100644 --- a/spring-web/src/test/java/org/springframework/web/method/support/ModelAndViewContainerTests.java +++ b/spring-web/src/test/java/org/springframework/web/method/support/ModelAndViewContainerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -58,7 +58,9 @@ public class ModelAndViewContainerTests { } @Test + @SuppressWarnings("deprecation") public void redirectScenarioWithoutRedirectModel() { + this.mavContainer.setIgnoreDefaultModelOnRedirect(false); this.mavContainer.addAttribute("name", "value"); this.mavContainer.setRedirectModelScenario(true); @@ -68,7 +70,6 @@ public class ModelAndViewContainerTests { @Test public void ignoreDefaultModel() { - this.mavContainer.setIgnoreDefaultModelOnRedirect(true); this.mavContainer.addAttribute("name", "value"); this.mavContainer.setRedirectModelScenario(true); @@ -77,7 +78,6 @@ public class ModelAndViewContainerTests { @Test // SPR-14045 public void ignoreDefaultModelAndWithoutRedirectModel() { - this.mavContainer.setIgnoreDefaultModelOnRedirect(true); this.mavContainer.setRedirectModelScenario(true); this.mavContainer.addAttribute("name", "value"); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java index 5de02d75617..8ffc1485e9e 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java @@ -176,7 +176,7 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter private ReactiveAdapterRegistry reactiveAdapterRegistry = ReactiveAdapterRegistry.getSharedInstance(); - private boolean ignoreDefaultModelOnRedirect = false; + private boolean ignoreDefaultModelOnRedirect = true; private int cacheSecondsForSessionAttributeHandlers = 0; @@ -465,7 +465,7 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter } /** - * By default the content of the "default" model is used both during + * By default, the content of the "default" model is used both during * rendering and redirect scenarios. Alternatively a controller method * can declare a {@link RedirectAttributes} argument and use it to provide * attributes for a redirect. @@ -474,10 +474,12 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter * is not declared. Setting it to {@code false} means the "default" model * may be used in a redirect if the controller method doesn't declare a * RedirectAttributes argument. - *

The default setting is {@code false} but new applications should - * consider setting it to {@code true}. + *

As of 6.0, this property is set to {@code true} by default. * @see RedirectAttributes + * @deprecated as of 6.0 without a replacement; once removed, the default + * model will always be ignored on redirect */ + @Deprecated public void setIgnoreDefaultModelOnRedirect(boolean ignoreDefaultModelOnRedirect) { this.ignoreDefaultModelOnRedirect = ignoreDefaultModelOnRedirect; } @@ -848,6 +850,7 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter * @since 4.2 * @see #createInvocableHandlerMethod(HandlerMethod) */ + @SuppressWarnings("deprecation") @Nullable protected ModelAndView invokeHandlerMethod(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) throws Exception { diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParserTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParserTests.java index d76ee7bf8ae..6c79844d34d 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParserTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParserTests.java @@ -74,7 +74,8 @@ public class AnnotationDrivenBeanDefinitionParserTests { ((ConfigurableWebBindingInitializer) initializer).getMessageCodesResolver(); assertThat(resolver).isNotNull(); assertThat(resolver.getClass()).isEqualTo(TestMessageCodesResolver.class); - assertThat(new DirectFieldAccessor(adapter).getPropertyValue("ignoreDefaultModelOnRedirect")).asInstanceOf(BOOLEAN).isFalse(); + assertThat(new DirectFieldAccessor(adapter).getPropertyValue("ignoreDefaultModelOnRedirect")) + .asInstanceOf(BOOLEAN).isTrue(); } @Test diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java index d6bdc86aaaf..c52eea70407 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java @@ -211,7 +211,8 @@ public class MvcNamespaceTests { RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class); assertThat(adapter).isNotNull(); - assertThat(new DirectFieldAccessor(adapter).getPropertyValue("ignoreDefaultModelOnRedirect")).asInstanceOf(BOOLEAN).isFalse(); + assertThat(new DirectFieldAccessor(adapter).getPropertyValue("ignoreDefaultModelOnRedirect")) + .asInstanceOf(BOOLEAN).isTrue(); List> converters = adapter.getMessageConverters(); assertThat(converters.size() > 0).isTrue(); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java index e5e9a93b65b..2232f77a25a 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java @@ -199,7 +199,7 @@ public class WebMvcConfigurationSupportExtensionTests { @SuppressWarnings("unchecked") @Test - public void requestMappingHandlerAdapter() throws Exception { + public void requestMappingHandlerAdapter() { RequestMappingHandlerAdapter adapter = this.config.requestMappingHandlerAdapter( this.config.mvcContentNegotiationManager(), this.config.mvcConversionService(), this.config.mvcValidator()); @@ -241,7 +241,7 @@ public class WebMvcConfigurationSupportExtensionTests { (DeferredResultProcessingInterceptor[]) fieldAccessor.getPropertyValue("deferredResultInterceptors"); assertThat(deferredResultInterceptors.length).isEqualTo(1); - assertThat(fieldAccessor.getPropertyValue("ignoreDefaultModelOnRedirect")).asInstanceOf(BOOLEAN).isFalse(); + assertThat(fieldAccessor.getPropertyValue("ignoreDefaultModelOnRedirect")).asInstanceOf(BOOLEAN).isTrue(); } @Test diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ModelAndViewMethodReturnValueHandlerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ModelAndViewMethodReturnValueHandlerTests.java index 906df78d022..27a93e1e500 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ModelAndViewMethodReturnValueHandlerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ModelAndViewMethodReturnValueHandlerTests.java @@ -146,8 +146,6 @@ public class ModelAndViewMethodReturnValueHandlerTests { @Test // SPR-14045 public void handleRedirectWithIgnoreDefaultModel() throws Exception { - mavContainer.setIgnoreDefaultModelOnRedirect(true); - RedirectView redirectView = new RedirectView(); ModelAndView mav = new ModelAndView(redirectView, "name", "value"); handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java index 32ecd653d70..811922635a0 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java @@ -135,7 +135,6 @@ public class RequestMappingHandlerAdapterTests { this.handlerAdapter.setArgumentResolvers(Arrays.asList(redirectAttributesResolver, modelResolver)); this.handlerAdapter.setReturnValueHandlers(Collections.singletonList(viewHandler)); - this.handlerAdapter.setIgnoreDefaultModelOnRedirect(true); this.handlerAdapter.afterPropertiesSet(); this.request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());