拦截器的类的写法:
public class ServletInterceptor extends AbstractInterceptor{
//重写intercept 方法
public String intercept(ActionInvocation ai) throws Exception {
Object o = ai.getAction();
//得到相应action的对象
}
1.配置全局的自定义拦截器,先声明自定义拦截器在strtus.xml文件中:
<package name="base" extends="struts-default">
<!--定义自己的拦截器-->
<interceptors>
<interceptorname="authority" class="com.gwideal.common.ActionInterceptors"/>
<!-- 定义默认拦截器-->
<interceptor-stackname="mydefault">
<interceptor-refname="defaultStack"/>
<interceptor-refname="authority"/>
</interceptor-stack>
</interceptors>
</package>
然后调用:
在其他的struts的配置文件中继承以上的文件(即base)
<package name="default" extends="base">
2.配置单个的自定义拦截器:
<package name="base" extends="struts-default">
<!--定义自己的拦截器-->
<interceptors>
<interceptorname="authority" class="com.gwideal.common.ActionInterceptors"/>
<!-- 定义默认拦截器-->
<interceptor-stackname="mydefault">
<interceptor-refname="defaultStack"/>
<interceptor-refname="authority"/>
</interceptor-stack>
</interceptors>
</package>
调用:
在你想用的拦截器的那个struts的配置文件中调用
<action name="Login"
class="com.example.struts.action.LoginAction" >
<result name="input">/jsp/login.jsp</result>
<result name="success">/jsp/success.jsp</result>
<!-- 使用自定义拦截器-->
<default-interceptor-refname="mydefault"/>
</interceptor-ref>
</action>
总结:全局拦截器的配置
在struts.xml中添加如下配置:
<!-- 配置全局拦截器 -->
<package name="all" extends="struts-default">
<interceptors>
<!-- 定义权限控制拦截器 -->
<interceptor name="authority"
class="akai.cost.ms.base.AuthInterceptor" />
<!-- 定义一个包含权限控制的拦截器栈 -->
<interceptor-stack name="mydefault">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="authority" />
</interceptor-stack>
</interceptors>
<!-- 定义默认拦截器 -->
<default-interceptor-ref name="mydefault" />
<!-- 定义全局处理结果 -->
<global-results>
<!-- 逻辑名为login的结果,映射到/login.jsp页面 -->
<result name="login">/login.jsp</result>
</global-results>
</package>
使用方法:其他包继承这个包名就可以了
<package name="abc" extends="all" namespace="/">