1

我正在尝试使用spring AOP编写一个拦截器。拦截器将查找请求URL是否是书签,如果是则重定向到身份验证页面。代码片段:

公共对象调用(MethodInvocation 调用)抛出 Throwable { logger.entering(this.getClass().getSimpleName(), "invoke", invocation);

    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;
}

当我调试控件按预期进入 else 块时,但在我返回结果后,控件转到加书签的 URl ratehr 的句柄方法,而不是重定向视图的处理程序。

请帮助我..提前谢谢。

4

1 回答 1

1

为什么拦截器需要 AOP。您可以使用常规拦截器轻松重定向。

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;

    }
}
于 2012-05-03T07:48:20.810 回答