Skip to content

Commit

Permalink
Fix for ModelAndView.status not working with RedirectView
Browse files Browse the repository at this point in the history
  • Loading branch information
jkatada committed Jun 15, 2020
1 parent 5225a57 commit 5c30b3c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Expand Up @@ -1404,6 +1404,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
Expand Up @@ -1855,6 +1855,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 @@ -3759,6 +3771,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 5c30b3c

Please sign in to comment.