gpt4 book ai didi

spring - Spring中的HTTP重定向: 301 (permanent) vs. 302(临时)

转载 作者:行者123 更新时间:2023-12-02 00:50:26 25 4
gpt4 key购买 nike

我想在 Spring 中进行 301 重定向,所以这里是我使用的代码

@RequestMapping(value = { "/devices" } , method = RequestMethod.GET)
private String initGetForm(@ModelAttribute("searchForm") final SearchForm searchForm,
BindingResult result,
HttpServletRequest request,
HttpServletResponse response,
Model model, Locale locale) throws Exception {


String newUrl = "/devices/en";

response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", newUrl);
response.setHeader("Connection", "close");

return "redirect:" + newUrl;

}

但是检查 IE 开发工具时我得到了这个状态 302 暂时移动!

最佳答案

Spring 在处理重定向时会重置您的响应 header ,因为您返回的是带有特殊重定向前缀的逻辑 View 名称。如果您想手动设置 header ,请自行处理响应而不使用 Spring View 解析。更改您的代码如下

@RequestMapping(value = { "/devices" } , method = RequestMethod.GET)
private void initGetForm(@ModelAttribute("searchForm") final SearchForm searchForm,
BindingResult result,
HttpServletRequest request,
HttpServletResponse response,
Model model, Locale locale) throws Exception {

String newUrl = request.getContextPath() + "/devices/en";
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", newUrl);
response.setHeader("Connection", "close");

}

关于spring - Spring中的HTTP重定向: 301 (permanent) vs. 302(临时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37182547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com