自定义拦截器
1). 具体步骤
I. 定义一个拦截器的类
有两种方式:
方式一: 可以实现 Interceptor 接口
方式二: 继承 AbstractInterceptor 抽象类
II. 在 struts.xml 文件配置.
<interceptors>
<interceptor name="hello" class="com.atguigu.struts2.interceptors.MyInterceptor"></interceptor>
</interceptors>
<action name="testToken" class="com.atguigu.struts2.token.app.TokenAction">
<interceptor-ref name="hello"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result>/success.jsp</result>
<result name="invalid.token">/token-error.jsp</result>
</action>
III. 注意: 在自定义的拦截器中可以选择不调用 ActionInvocation 的 invoke() 方法. 那么后续的拦截器和 Action 方法将不会被调用.
Struts 会渲染自定义拦截器 intercept 方法返回值对应的 result
自定义拦截器的步骤
最新推荐文章于 2024-06-16 21:39:34 发布