struts.xml的配置
<interceptors>
<interceptor name="LoginInterceptor" class="com.jeizas.Interceptor.LoginInterceptor"></interceptor>
<interceptor-stack name="LoginInterceptorStack">
<interceptor-ref name="LoginInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<interceptor-ref name="LoginInterceptorStack"><interceptor-ref>
java文件配置
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LoginInterceptor extends AbstractInterceptor {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation arg0) throws Exception {
// TODO Auto-generated method stub
String actionName = arg0.getInvocationContext().getName();//获得当前要调用的action
if(actionName == "LoginUserAction"){//登录所用的action放行
return arg0.invoke();
}
Map<String,Object> session=ActionContext.getContext().getSession();
Object user=session.get("loginUser");//从session中获得登录用户
if(user != null){
return arg0.invoke();
}else{
return Action.LOGIN;
}
}
}