在 Spring 3 控制器中返回字符串时为重定向设置 HTTP 状态代码 [英] Set HTTP status code for redirect when returning a String in a Spring 3 controller
问题描述
在 Spring 3 中返回 "redirect:/new/url"
时有没有办法指定 HTTP 状态码?
Is there a way to specify the HTTP status code when returning "redirect:/new/url"
in Spring 3?
推荐答案
没试过,但是看org.springframework.web.servlet.view.RedirectView
的源码有getHttp11StatusCode()
确定响应的 HTTP 状态的方法.
Haven't tried it, but looking at the source of org.springframework.web.servlet.view.RedirectView
it has getHttp11StatusCode()
method that determines the HTTP status of response.
似乎您可以根据请求通过设置 org.springframework.web.servlet.View#RESPONSE_STATUS_ATTRIBUTE
属性来覆盖默认响应代码.简单设置:
It seems like you can override the default response code by settings org.springframework.web.servlet.View#RESPONSE_STATUS_ATTRIBUTE
property on request. Simply set:
httpServletRequest.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, status)
在返回redirect:
"视图之前.
这篇关于在 Spring 3 控制器中返回字符串时为重定向设置 HTTP 状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!