配置struts2拦截器
1. 编写拦截器的类
如:com.wfhms.system.filter.LoginInterceptor
//继承AbstractInterceptor类
publicclass LoginInterceptorextends AbstractInterceptor {
//重写intercept方法
@Override
public String intercept(ActionInvocation invocation)throws Exception {
//验证session中是否有登陆的user
Map<String, Object> session = invocation.getInvocationContext().getSession();
if (session.get("user") ==null) {
//不存在返回的路径
return"login";
}
return invocation.invoke();
}
}
2.在struts.xml添加如下
<packagename="default"extends="json-default">
<interceptors>
<!—引入编写的类,name和背景红色对应 -->
<interceptorname="loginInterceptor"class="com.wfhms.system.filter.LoginInterceptor">
</interceptor>
<interceptor-stackname="defaultStack">
<interceptor-refname="exception"/>
<interceptor-refname="alias"/>
<interceptor-refname="prepare"/>
<interceptor-refname="i18n"/>
<interceptor-refname="chain"/>
<interceptor-refname="fileUpload"/>
<interceptor-refname="params"/>
<interceptor-refname="conversionError"/>
<interceptor-refname="loginInterceptor"/>
</interceptor-stack>
</interceptors>
<!—-默认跳转的action -->
<default-action-refname="login"/>
<global-results>
<resultname="login"type="redirectAction">login</result>
</global-results>
</package>