1.Spring的aop来验证用户是否已经登录的拦截器
package angus.interceptor; import org.aopalliance.intercept.*; import javax.servlet.http.*; import org.apache.struts.action.*; import angus.vo.ClientVO; public class ClientInterceptor implements MethodInterceptor { public Object invoke(MethodInvocation invocation)throws Throwable { HttpServletRequest request = null; ActionMapping mapping = null; Object[] args = invocation.getArguments(); for (int i = 0 ; i < args.length ; i++ ) { if (args[i] instanceof HttpServletRequest) { request = (HttpServletRequest)args[i]; } if (args[i] instanceof ActionMapping) { mapping = (ActionMapping)args[i]; } } HttpSession session = request.getSession(); ClientVO cvo = (ClientVO)session.getAttribute("clientInfo"); if (cvo == null) { request.setAttribute("pleaseLogin", "请先登录"); String url = request.getRequestURI() + "?" + request.getQueryString(); session.setAttribute("clientUrl", url); request.setAttribute("nologin", "请先登录"); ActionForward af = new ActionForward("/myalbum.do"); return af; } else { return invocation.proceed(); } } }
2.相应的在Spring中的applicationContext.xml中的配置
<bean id="commentInter" class="angus.interceptor.CommentInterceptor"/>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>/addComment</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>commentInter</value>
</list>
</property>
</bean>