Skip to content

Commit c0f79ee

Browse files
committed
Merge pull request #25092 from jkatada:fix-ModelAndView-status-for-redirect
* gh-25092: Fix for ModelAndView.status not working with RedirectView
2 parents 14f24f4 + 9261766 commit c0f79ee

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,7 @@ protected void render(ModelAndView mv, HttpServletRequest request, HttpServletRe
13951395
}
13961396
try {
13971397
if (mv.getStatus() != null) {
1398+
request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, mv.getStatus());
13981399
response.setStatus(mv.getStatus().value());
13991400
}
14001401
view.render(mv.getModelInternal(), request, response);

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,6 +1897,18 @@ void modelAndViewWithStatus(boolean usePathPatterns) throws Exception {
18971897
assertThat(response.getForwardedUrl()).isEqualTo("view");
18981898
}
18991899

1900+
@PathPatternsParameterizedTest
1901+
void modelAndViewWithStatusForRedirect(boolean usePathPatterns) throws Exception {
1902+
initDispatcherServlet(ModelAndViewController.class, usePathPatterns);
1903+
1904+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/redirect");
1905+
MockHttpServletResponse response = new MockHttpServletResponse();
1906+
getServlet().service(request, response);
1907+
1908+
assertThat(response.getStatus()).isEqualTo(307);
1909+
assertThat(response.getRedirectedUrl()).isEqualTo("/path");
1910+
}
1911+
19001912
@PathPatternsParameterizedTest // SPR-14796
19011913
void modelAndViewWithStatusInExceptionHandler(boolean usePathPatterns) throws Exception {
19021914
initDispatcherServlet(ModelAndViewController.class, usePathPatterns);
@@ -3872,6 +3884,11 @@ public ModelAndView methodWithHttpStatus(MyEntity object) {
38723884
return new ModelAndView("view", HttpStatus.UNPROCESSABLE_ENTITY);
38733885
}
38743886

3887+
@RequestMapping("/redirect")
3888+
public ModelAndView methodWithHttpStatusForRedirect(MyEntity object) {
3889+
return new ModelAndView("redirect:/path", HttpStatus.TEMPORARY_REDIRECT);
3890+
}
3891+
38753892
@RequestMapping("/exception")
38763893
public void raiseException() throws Exception {
38773894
throw new TestException();

0 commit comments

Comments
 (0)