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 May 17, 2020
1 parent 212bb7f commit b72f87a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
Expand Down Expand Up @@ -1368,6 +1368,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 @@ -1795,6 +1795,18 @@ public void modelAndViewWithStatus() throws Exception {
assertThat(response.getForwardedUrl()).isEqualTo("view");
}

@Test
public void modelAndViewWithStatusForRedirect() throws Exception {
initServletWithControllers(ModelAndViewController.class);

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

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

@Test // SPR-14796
public void modelAndViewWithStatusInExceptionHandler() throws Exception {
initServletWithControllers(ModelAndViewController.class);
Expand Down Expand Up @@ -3699,6 +3711,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 b72f87a

Please sign in to comment.