[java]代码库
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LoginInterceptor extends AbstractInterceptor {
public String intercept (ActionInvocation invocation) throws Exception {
String statte = (String) ServletActionContext.getRequest().getSession().getAttribute ("login_state"); //获取登陆状态
if (statte != null) { //判断是否为空
return invocation.invoke(); //如果为是,继续执行ACTION方法
} else {
return "backerror"; //如果为否,弹出
}
}
}
<interceptors>
<interceptor name="authority" class="org.yzsoft.examdemo.action.LoginInterceptor"></interceptor>
<!-- 拦截器栈 -->
<interceptor-stack name="myDefault">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="authority"></interceptor-ref>
</interceptor-stack>
</interceptors>
<global-results>
<!-- 当返回error视图名时,转入/error.jsp页面 -->
<result name="backerror">error.jsp</result>
<result name="fronterror">login.jsp</result>
</global-results>