2012-05-03 80 views
1

我試圖用spring AOP編寫攔截器。攔截器會發現請求URL是否爲書籤,如果是,將重定向到認證頁面。 代碼段:使用AOP-Spring攔截器的書籤標記重定向

公共對象調用(的MethodInvocation調用)拋出的Throwable { logger.entering(this.getClass()getSimpleName(), 「調用」,調用。);

Object result = null; 
    try { 
     // Logic to exclude the beans as per the list in the configuration. 
     boolean excluded = false; 
     for (String excludebean : excludedBeans) { 
      if (excludebean != null && excludebean.equalsIgnoreCase(invocation.getThis().getClass().getSimpleName())) { 
       excluded = true; 
       break; 
      } 
     } 

     // If the Target Method is "toString", then set EXCLUDE to TRUE and process the request 
     if(excluded == false && invocation.getMethod().getName().equalsIgnoreCase("toString")) 
     { 
      excluded = true; 
     } 

     // if user session object is available, then process the request or 
     // else forward to the configured view. 
     if (excluded || getSessionHolder().getUserVO() != null) { 
      result = invocation.proceed(); 
     } 
     else { 
      logger.logp(Level.INFO, this.getClass().getSimpleName(), 
        "invoke(MethodInvocation)", "User Object is "+ getSessionHolder().getUserVO() 
          + ". So redirecting user to home page"); 
      result = new ModelAndView("redirect:/security/authenticate.do"); 

     } 
    } 
    catch (Throwable ex) { 
     throw ex; 
    } 
    logger.exiting(this.getClass().getSimpleName(), "invoke"); 
    return result; 
} 

當我調試控制而來的其他塊內的預期,但之後我返回結果,控制轉到用於書籤網址ratehr比處理程序重定向視圖handle方法。

請幫我對此..預先感謝。

回答

1

爲什麼你需要攔截器的AOP。您可以使用Regular攔截器輕鬆重定向。

public class RedirectInterceptor extends HandlerInterceptorAdapter{ 

    private String redirectMapping; 

    public void setRedirectMapping(String redirectMapping) { 
     this.redirectMapping = **maintenanceMapping**; 
    } 


    //before the actual handler will be executed 
    public boolean preHandle(HttpServletRequest request, 
      HttpServletResponse response, Object handler) 
     throws Exception { 
         if (somethingHappened){ 
      response.sendRedirect(redirectMapping); 
      return false; 
         } else 
          return true; 

    } 
} 
+0

謝謝丹尼,我會試試這個。 – RVP

+0

我試過這種方式,我在攔截器內部有一個調試點,但控制器永遠不會進入內部,我已將攔截器添加到中。我正在使用基於註解的url mapping.pls幫助。 – RVP

+0

請張貼您的代碼和配置xml –