2016-01-15 58 views
0

我使用spring 3.x和tomcat 7 我有一個帶有星號映射的控制器,它具有檢測重定向路徑的方法。 這裏:在控制器的請求映射方法上避免302重定向

@RequestMapping(value = "/**/t") 
public class TopCategoryPageController extends AbstractSearchPageController 
{ 
... 
    //@ResponseStatus(HttpStatus.MOVED_PERMANENTLY/* this is 301 */) 
    @RequestMapping(value = TOP_CATEGORY_CODE_PATH_VARIABLE_PATTERN, method = RequestMethod.GET) 
    public String topCategory(@PathVariable("categoryCode") final String categoryCode, final Model model, 
      final HttpServletRequest request, final HttpServletResponse response) 
        throws UnsupportedEncodingException, CMSItemNotFoundException 
    { 
    ... 
    if any redirection 
    { 
     response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); 
     return redirection; 
    } 
    else 
    return getViewForPage(categoryCode); 
} 

當請求到來時,它執行上述

另外第一topCategory方法,我有一個攔截器如下面。它按照預期在topCategory方法之後執行。雖然我試圖在那裏插入301狀態,但我無法得到我想要的。

這裏:

public class BeforeViewHandlerInterceptor extends HandlerInterceptorAdapter 
{ 
    ... 
    public void postHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler, 
      final ModelAndView modelAndView) throws Exception 
    { 
    ... 
      if (isRedirectView(modelAndView)) 
     { 
      String uri = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() 
        + request.getContextPath(); 
      uri += modelAndView.getViewName().substring(modelAndView.getViewName().indexOf(":") + 1, 
        modelAndView.getViewName().length()); 
      modelAndView.setViewName("redirect:" + uri); 
      response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); 
      response.setHeader("Location", response.encodeRedirectURL(uri)); 
     } 
    } 

Neverthless設定的postHandle 301個狀態。它仍然重定向與302.

這是我點擊我的網頁上的鏈接,如:domain/context/t/categoryCode 當我分析鉻網絡選項卡時,我看到第一個重定向302與其他發起人。第二個是301.請注意,我需要首先重定向301.所以,我找不到它重定向到302的地方。Thx和brgds

回答

0

當我將代碼插入到如下所示的控制器片段中時,正常。

@RequestMapping(value = "/**/t") 
public class TopCategoryPageController extends AbstractSearchPageController 
{ 
... 
    @RequestMapping(value = TOP_CATEGORY_CODE_PATH_VARIABLE_PATTERN, method = RequestMethod.GET) 
    public ModelAndView topCategory(@PathVariable("categoryCode") final String categoryCode, final Model model, 
      final HttpServletRequest request, final HttpServletResponse response) 
        throws UnsupportedEncodingException, CMSItemNotFoundException 
    { 
    ... 
    if any redirection 
    { 
     final RedirectView redirectView = new RedirectView(redirection); 
     redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY); 
     return new ModelAndView(redirectView); 
    } 
    else 
     return getViewForPage(categoryCode); 
}