Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModelAndView.status does not work with RedirectView #25092

Conversation

jkatada
Copy link
Contributor

@jkatada jkatada commented May 17, 2020

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]

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 17, 2020
@jkatada jkatada force-pushed the fix-ModelAndView-status-for-redirect branch from b72f87a to 5c30b3c Compare June 15, 2020 13:19
@rstoyanchev rstoyanchev added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Nov 10, 2021
@poutsma poutsma self-assigned this Dec 7, 2021
@poutsma poutsma added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 7, 2021
@poutsma poutsma added this to the 5.3.14 milestone Dec 7, 2021
poutsma added a commit that referenced this pull request Dec 7, 2021
…direct

* gh-25092:
  Fix for ModelAndView.status not working with RedirectView
@poutsma
Copy link
Contributor

poutsma commented Dec 7, 2021

Merged in c0f79ee.

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

@poutsma poutsma closed this Dec 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants