使用struts2 拦截器实现判断是否登录功能
package com.iapppay.maintain.common;
import java.util.Map;
import org.jasypt.commons.CommonUtils;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class LoginInterceptor extends MethodFilterInterceptor {
private static final long serialVersionUID = 3590018080122600795L;
@Override
protected String doIntercept(ActionInvocation arg0) throws Exception {
ActionContext act=arg0.getInvocationContext();
Map session = act.getSession();
String user=(String) session.get("user");
if(!CommonUtils.isEmpty(user)){
return arg0.invoke();
}
return Action.LOGIN;
}
}
struts.xml配置
<interceptors>
<interceptor name="loginInterceptor" class="com.iapppay.maintain.common.LoginInterceptor" />
<interceptor-stack name="maintainInterceptor">
<interceptor-ref name="loginInterceptor">
<param name="excludeMethods">index,login</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="maintainInterceptor" />