+-
HTTP重定向:Spring中的301(永久)与302(临时)
我想在 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正在处理重定向时重置响应标头,因为您返回的逻辑视图名称带有特殊的重定向前缀.如果您想手动设置标头,请自行处理响应,而不使用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");

    }
点击查看更多相关文章

转载注明原文:HTTP重定向:Spring中的301(永久)与302(临时) - 乐贴网