ITeye博客

利用spring拦截struts(1.x)中的action

maybe723 · 2008-10-26 18:14 · struts+spring+hibernate · Markdown

在struts-config.xml中配置:

 <controller
  processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
 </controller>

 新建AuthMethodInteceptor:

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.struts.action.ActionMapping;

import entity.Users;

public class AuthMethodInterceptor implements MethodInterceptor {

	public Object invoke(MethodInvocation invocation) throws Throwable {
		Object[] object=invocation.getArguments();
		try
		{
			HttpServletRequest request = (HttpServletRequest) invocation.getArguments()[2];
			ActionMapping mapping=(ActionMapping) invocation.getArguments()[0];
			Users user = (Users) (request.getSession().getAttribute("user"));
			
			if(user==null)
			{
			     return mapping.findForward("login");
		
			}
			Object returnObject = invocation.proceed();
		                return returnObject;

		}
		catch(Exception e)
		{
			
		}
		
		return object;
	}
}

 

   spring配置文件中:

 

 

<bean id="authInterceptor" class="utils.AuthMethodInterceptor">

	</bean>


	<bean
		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="interceptorNames">
			<list>
				<value>authInterceptor</value>
			</list>
		</property>
		<property name="beanNames">
			<list>
				<value>/*insert</value>

				
			</list>
		</property>

	</bean>

评论(1)

  1. #1 楼 · nashwill · 2011-10-30 03:22

    测试了下,好像用不了,什么原因?

评论功能维护中,当前仅展示历史评论。