struts.xml 中
<interceptors>
<interceptor name="login" class="com.lz.interceptor.CheckLoginInterceptor"/>
<interceptor-stack name="teamwareStack">
<interceptor-ref name="login"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
实现类:
package com.lz.interceptor;
import java.util.Map;
import com.lz.action.UsersAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class CheckLoginInterceptor extends AbstractInterceptor {
public static final String LOGIN_KEY = "login";
@Override
public String intercept(ActionInvocation arg0) throws Exception {
Object action = arg0.getAction();
if (action instanceof UsersAction) {
return arg0.invoke();
}
Map<String, Object> session = arg0.getInvocationContext().getSession();
String login = (String)session.get(LOGIN_KEY);
if(login != null && login.length() > 0){
return arg0.invoke();
} else{
return "error";
}
}
}