拦截器:拦截器类似于过滤器的功能,过滤器可以过滤项目的任何请求(servlet/jsp/html/img),拦截器只能拦截Action请求。
定义拦截器interceptor :实现Interceptor接口 实现必须的三个方法
init() :拦截器初始化 服务器启动时候就初始化所有需要的拦截器
intercept(ActionInvocation invocation):拦截 action访问的时候执行
destroy():销毁 服务器停止的时候销毁
申明和使用拦截器:
<package name="inter" extends="struts-default" namespace="/inter">
<!-- 申明拦截器:拦截器在此申明后,在web服务器启动的时候就会被初始化 -->
<interceptors>
<interceptor name="inter1" class="com.example.MyInterceptor1"></interceptor>
</interceptors>
<action name="user_*" class="com.example.UserAction" method="{1}">
<!-- 申明该action使用该拦截器:使用后,访问该action的请求,会先经过拦截器 -->
<interceptor-ref name="inter1"></interceptor-ref>
<result>/index.jsp</result>
</action>
</package>拦截器:拦截器类似于过滤器的功能,过滤器可以过滤项目的任何请求(servlet/jsp/htm