struts2 配置拦截器

时间:2023-03-08 23:35:18
struts2 配置拦截器

第一步:继承MethodFilterInterceptor写自己的自定义拦截器

 import org.apache.struts2.ServletActionContext;

 import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; /**
* 登录的拦截器
* @author anye
*
*/
public class LoginInterceptor extends MethodFilterInterceptor{
private static final long serialVersionUID = 1L; @Override
protected String doIntercept(ActionInvocation arg0) throws Exception {
Object object = ServletActionContext.getRequest().getSession().getAttribute("admin");
if(object==null){
return Action.LOGIN;
}
return arg0.invoke();//递归调用拦截器
} }

第二步:在stuts.xml文件中配置自定义拦截器,并将自定义拦截器加入默认拦截器中,设置默认拦截器。

<!-- 拦截器 -->
<interceptors>
<interceptor name="login" class="com.tcrj.interceptor.LoginInterceptor"></interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="login">
<param name="excludeMethods">login</param>
</interceptor-ref>
<interceptor-ref name="paramsPrepareParamsStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack" />