Browse Source

Deprecate ignoreDefaultModelOnRedirect property

Closes gh-28324
pull/29162/head
rstoyanchev 3 years ago
parent
commit
f3c082abac
  1. 141
      spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/MultipartControllerTests.java
  2. 119
      spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/MultipartControllerTests.java
  3. 9
      spring-web/src/main/java/org/springframework/web/method/support/ModelAndViewContainer.java
  4. 6
      spring-web/src/test/java/org/springframework/web/method/support/ModelAndViewContainerTests.java
  5. 11
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java
  6. 3
      spring-webmvc/src/test/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParserTests.java
  7. 3
      spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java
  8. 4
      spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java
  9. 2
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ModelAndViewMethodReturnValueHandlerTests.java
  10. 1
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java

141
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; @@ -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; @@ -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 { @@ -69,28 +66,18 @@ public class MultipartControllerTests {
bodyBuilder.part("file", fileContent).filename("orig");
bodyBuilder.part("json", json, MediaType.APPLICATION_JSON);
EntityExchangeResult<Void> 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 { @@ -110,16 +97,11 @@ public class MultipartControllerTests {
bodyBuilder.part("file", fileContent).filename("orig");
bodyBuilder.part("json", json, MediaType.APPLICATION_JSON);
EntityExchangeResult<Void> 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 { @@ -138,16 +120,11 @@ public class MultipartControllerTests {
bodyBuilder.part("file", fileContent).filename("orig");
bodyBuilder.part("json", json, MediaType.APPLICATION_JSON);
EntityExchangeResult<Void> 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 { @@ -157,16 +134,11 @@ public class MultipartControllerTests {
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
bodyBuilder.part("json", json, MediaType.APPLICATION_JSON);
EntityExchangeResult<Void> 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 { @@ -179,16 +151,11 @@ public class MultipartControllerTests {
bodyBuilder.part("file", fileContent).filename("orig");
bodyBuilder.part("json", json, MediaType.APPLICATION_JSON);
EntityExchangeResult<Void> 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 { @@ -198,16 +165,11 @@ public class MultipartControllerTests {
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
bodyBuilder.part("json", json, MediaType.APPLICATION_JSON);
EntityExchangeResult<Void> 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 { @@ -220,16 +182,11 @@ public class MultipartControllerTests {
bodyBuilder.part("file", fileContent).filename("orig");
bodyBuilder.part("json", json, MediaType.APPLICATION_JSON);
EntityExchangeResult<Void> 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 { @@ -239,16 +196,11 @@ public class MultipartControllerTests {
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
bodyBuilder.part("json", json, MediaType.APPLICATION_JSON);
EntityExchangeResult<Void> 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 { @@ -260,16 +212,11 @@ public class MultipartControllerTests {
bodyBuilder.part("file", fileContent).filename("orig");
bodyBuilder.part("json", json, MediaType.APPLICATION_JSON);
EntityExchangeResult<Void> 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 { @@ -283,15 +230,11 @@ public class MultipartControllerTests {
.filter(new RequestWrappingFilter())
.build();
EntityExchangeResult<Void> 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 { @@ -301,110 +244,78 @@ public class MultipartControllerTests {
@PostMapping("/multipartfile")
public String processMultipartFile(@RequestParam(required = false) MultipartFile file,
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
if (file != null) {
model.addAttribute("fileContent", file.getBytes());
}
if (json != null) {
model.addAttribute("jsonContent", json);
}
@RequestPart(required = false) Map<String, String> json) {
return "redirect:/index";
}
@PutMapping("/multipartfile-via-put")
public String processMultipartFileViaHttpPut(@RequestParam(required = false) MultipartFile file,
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
@RequestPart(required = false) Map<String, String> json) {
return processMultipartFile(file, json, model);
return processMultipartFile(file, json);
}
@PostMapping("/multipartfilearray")
public String processMultipartFileArray(@RequestParam(required = false) MultipartFile[] file,
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
@RequestPart(required = false) Map<String, String> 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<MultipartFile> file,
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
@RequestPart(required = false) Map<String, String> 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<MultipartFile> file,
@RequestPart Map<String, String> json, Model model) throws IOException {
if (file.isPresent()) {
model.addAttribute("fileContent", file.get().getBytes());
}
model.addAttribute("jsonContent", json);
public String processOptionalFile(
@RequestParam Optional<MultipartFile> file, @RequestPart Map<String, String> json) {
return "redirect:/index";
}
@PostMapping("/optionalfilearray")
public String processOptionalFileArray(@RequestParam Optional<MultipartFile[]> file,
@RequestPart Map<String, String> json, Model model) throws IOException {
public String processOptionalFileArray(
@RequestParam Optional<MultipartFile[]> file, @RequestPart Map<String, String> 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<List<MultipartFile>> file,
@RequestPart Map<String, String> json, Model model) throws IOException {
public String processOptionalFileList(
@RequestParam Optional<List<MultipartFile>> file, @RequestPart Map<String, String> 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<String, String> json, Model model) throws IOException {
model.addAttribute("fileContent", part.getInputStream());
model.addAttribute("jsonContent", json);
public String processPart(@RequestParam Part part, @RequestPart Map<String, String> json) {
return "redirect:/index";
}
@PostMapping("/json")
public String processMultipart(@RequestPart Map<String, String> json, Model model) {
model.addAttribute("json", json);
public String processMultipart(@RequestPart Map<String, String> json) {
return "redirect:/index";
}
}

119
spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/MultipartControllerTests.java

@ -40,8 +40,6 @@ import org.springframework.mock.web.MockPart; @@ -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; @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -263,128 +242,96 @@ class MultipartControllerTests {
MockMvc mockMvc = standaloneSetup(new MultipartController()).addFilter(filter).build();
Map<String, String> 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<String, String> json, Model model) throws IOException {
if (file != null) {
model.addAttribute("fileContent", file.getBytes());
}
if (json != null) {
model.addAttribute("jsonContent", json);
}
@RequestPart(required = false) Map<String, String> json) throws IOException {
return "redirect:/index";
}
@PutMapping("/multipartfile-via-put")
public String processMultipartFileViaHttpPut(@RequestParam(required = false) MultipartFile file,
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
@RequestPart(required = false) Map<String, String> 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<String, String> json, Model model) throws IOException {
@RequestPart(required = false) Map<String, String> 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<MultipartFile> file,
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
@RequestPart(required = false) Map<String, String> 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<MultipartFile> file,
@RequestPart Map<String, String> json, Model model) throws IOException {
if (file.isPresent()) {
model.addAttribute("fileContent", file.get().getBytes());
}
model.addAttribute("jsonContent", json);
public String processOptionalFile(
@RequestParam Optional<MultipartFile> file, @RequestPart Map<String, String> json) {
return "redirect:/index";
}
@PostMapping("/optionalfilearray")
public String processOptionalFileArray(@RequestParam Optional<MultipartFile[]> file,
@RequestPart Map<String, String> json, Model model) throws IOException {
public String processOptionalFileArray(
@RequestParam Optional<MultipartFile[]> file, @RequestPart Map<String, String> 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<List<MultipartFile>> file,
@RequestPart Map<String, String> json, Model model) throws IOException {
public String processOptionalFileList(
@RequestParam Optional<List<MultipartFile>> file, @RequestPart Map<String, String> 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<String, String> 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<String, String> json) {
return "redirect:/index";
}
@PostMapping("/json")
public String processMultipart(@RequestPart Map<String, String> json, Model model) {
model.addAttribute("json", json);
public String processMultipart(@RequestPart Map<String, String> 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 { @@ -396,6 +343,7 @@ class MultipartControllerTests {
}
}
private static class MultipartFileBean {
private MultipartFile file;
@ -414,8 +362,9 @@ class MultipartControllerTests { @@ -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);

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

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -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.
* <p>The default setting is {@code false}.
* <p>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;
}

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

@ -1,5 +1,5 @@ @@ -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 { @@ -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 { @@ -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 { @@ -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");

11
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java

@ -176,7 +176,7 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter @@ -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 @@ -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 @@ -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.
* <p>The default setting is {@code false} but new applications should
* consider setting it to {@code true}.
* <p>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 @@ -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 {

3
spring-webmvc/src/test/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParserTests.java

@ -74,7 +74,8 @@ public class AnnotationDrivenBeanDefinitionParserTests { @@ -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

3
spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java

@ -211,7 +211,8 @@ public class MvcNamespaceTests { @@ -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<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
assertThat(converters.size() > 0).isTrue();

4
spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java

@ -199,7 +199,7 @@ public class WebMvcConfigurationSupportExtensionTests { @@ -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 { @@ -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

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

@ -146,8 +146,6 @@ public class ModelAndViewMethodReturnValueHandlerTests { @@ -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);

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

@ -135,7 +135,6 @@ public class RequestMappingHandlerAdapterTests { @@ -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());

Loading…
Cancel
Save