Skip to content

Commit

Permalink
Merge pull request #25092 from jkatada:fix-ModelAndView-status-for-re…
Browse files Browse the repository at this point in the history
…direct

* gh-25092:
  Fix for ModelAndView.status not working with RedirectView
  • Loading branch information
poutsma committed Dec 7, 2021
2 parents 14f24f4 + 9261766 commit c0f79ee
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,7 @@ protected void render(ModelAndView mv, HttpServletRequest request, HttpServletRe
}
try {
if (mv.getStatus() != null) {
request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, mv.getStatus());
response.setStatus(mv.getStatus().value());
}
view.render(mv.getModelInternal(), request, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1897,6 +1897,18 @@ void modelAndViewWithStatus(boolean usePathPatterns) throws Exception {
assertThat(response.getForwardedUrl()).isEqualTo("view");
}

@PathPatternsParameterizedTest
void modelAndViewWithStatusForRedirect(boolean usePathPatterns) throws Exception {
initDispatcherServlet(ModelAndViewController.class, usePathPatterns);

MockHttpServletRequest request = new MockHttpServletRequest("GET", "/redirect");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);

assertThat(response.getStatus()).isEqualTo(307);
assertThat(response.getRedirectedUrl()).isEqualTo("/path");
}

@PathPatternsParameterizedTest // SPR-14796
void modelAndViewWithStatusInExceptionHandler(boolean usePathPatterns) throws Exception {
initDispatcherServlet(ModelAndViewController.class, usePathPatterns);
Expand Down Expand Up @@ -3872,6 +3884,11 @@ public ModelAndView methodWithHttpStatus(MyEntity object) {
return new ModelAndView("view", HttpStatus.UNPROCESSABLE_ENTITY);
}

@RequestMapping("/redirect")
public ModelAndView methodWithHttpStatusForRedirect(MyEntity object) {
return new ModelAndView("redirect:/path", HttpStatus.TEMPORARY_REDIRECT);
}

@RequestMapping("/exception")
public void raiseException() throws Exception {
throw new TestException();
Expand Down

0 comments on commit c0f79ee

Please sign in to comment.