首先配置struts.xml文件
<package name="intercepts" extends="struts-default">
<interceptors>
<!--这个是自定义的拦截器-->
<interceptor name="login" class="com.test.intercept.Login"></interceptor>
<!--定义一个拦截器栈,包含默认的拦截器和自定义拦截器-->
<interceptor-stack name="myStack">
<interceptor-ref name="login"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
<interceptors>
<default-interceptor-ref name="myStack" />
</package>
<package name="test" extends="intercepts" namespace="/test">
<!---->
<action name="login" class="com.test.Action.TestAction">
<result name="success">/aaa.jsp</result>
</action>
</package>
public class Login extends AbstractInterceptor
{
public String interceptor (ActionInvocation invocate) throws Exception
{
System.out.println("这个是拦截器");
ActionContext ctx = ActionContext.getContext() ;
Map session = ctx.getSession();
String url = actionInvocation.getInvocationContext().getName();
if(url.equals(login.action)){
invocate.invoke();
}
if(null==session.get("name")){
return "login" ;
}else{
return invocate.invoke();
}
}
}
然后在地址栏里输入:http://localhost:8080/ceload/login.action