When setting a redirect view name and a status code to ModelAndView, the status code is ignored.

@PostMapping("/modelandviewredirect1")
public ModelAndView modelandviewredirect1(UserBean user) {
    return new ModelAndView("redirect:/result", HttpStatus.TEMPORARY_REDIRECT); // set:<307>, but was:<302>
}

The workarounds for this problem are as follows:

// 1: use RedirectView#setStatusCode(..)
@PostMapping("/modelandviewredirectview1")
public ModelAndView modelandviewredirectview1(UserBean user) {
    RedirectView view = new RedirectView("/result", true);
    view.setStatusCode(HttpStatus.TEMPORARY_REDIRECT);
    return new ModelAndView(view);
}

// 2: use the RESPONSE_STATUS_ATTRIBUTE request attribute
@PostMapping("/modelandviewredirect2")
public ModelAndView modelandviewredirect2(UserBean user, HttpServletRequest request) {
    request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.TEMPORARY_REDIRECT);
    return new ModelAndView("redirect:/result");
}

@ResponseStatus is supported with RedirectView as result of #10812 and #17800 resolution. I think ModelAndView.status should be supported with RedirectView as well as @ResponseStatus.

This PR fixes the problem by passing ModelAndView.status to the RESPONSE_STATUS_ATTRIBUTE request attribute.

Related issues

#10812 @ResponseStatus annotation is ignored in an @Controller redirect (RedirectView) [SPR-6144] #17800 Make RedirectViews use RESPONSE_STATUS_ATTRIBUTE as a response status if defined [SPR-13208] #18136 ability to set response status on ModelAndView [SPR-13560] #19362 ModelAndView's setStatus does not work for @ExceptionHandler methods [SPR-14796]

Comment From: poutsma

Merged in c0f79ee00acb91371544d72feab235dec5737072.

Thanks for submitting a PR, and we are sorry it took this long to resolve.