Nuñ*_*ada 3 redirect spring spring-mvc
我想在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;
}
Run Code Online (Sandbox Code Playgroud)
但是检查IE开发者工具我得到了这个状态302暂时移动!
Spring正在处理重定向时重置响应标头,因为您返回的逻辑视图名称带有特殊的重定向前缀.如果您想手动设置标头,请自行处理响应,而不使用Spring视图解析.更改您的代码如下
@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");
}
Run Code Online (Sandbox Code Playgroud)
您可以将RedirectView与TEMPORARY_REDIRECT状态一起使用。
@RequestMapping(value = { "/devices" } , method = RequestMethod.GET)
private ModelAndView initGetForm(@ModelAttribute("searchForm") final SearchForm searchForm,
BindingResult result,
HttpServletRequest request,
HttpServletResponse response,
Model model, Locale locale) throws Exception {
....
RedirectView redirectView = new RedirectView(url);
redirectView.setStatusCode(HttpStatus.TEMPORARY_REDIRECT);
return new ModelAndView(redirectView);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2843 次 |
| 最近记录: |