[原创]java WEB学习笔记74:Struts2 学习之路--自定义拦截器,struts内建的拦截器

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

本人互联网技术爱好者,互联网技术发烧友

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1.Struts2 拦截器

  1)拦截器(Interceptor)是 Struts 2 的核心组成部分。

  2)Struts2 很多功能都是构建在拦截器基础之上的,例如文件的上传和下载、国际化、数据类型转换和数据校验等等。

  3)Struts2 拦截器在访问某个 Action 方法之前或之后实施拦截 

  4)Struts2 拦截器是可插拔的, 拦截器是 AOP(面向切面编程) 的一种实现.

  5)拦截器栈(Interceptor Stack): 将拦截器按一定的顺序联结成一条链. 在访问被拦截的方法时, Struts2 拦截器链中的拦截器就会按其之前定义的顺序被依次调用

2.strut2 的拦截器模型

           

3.Struts2 自带的拦截器

  

  

4.Interceptor 接口

  1)每个拦截器都是实现了 接口的 Java 类:

    

    - init: 该方法将在拦截器被创建后立即被调用, 它在拦截器的生命周期内只被调用一次. 可以在该方法中对相关资源进行必要的初始化

    - interecept: 每拦截一个请求, 该方法就会被调用一次.

    - destroy: 该方法将在拦截器被销毁之前被调用, 它在拦截器的生命周期内也只被调用一次.

  2)Struts 会依次调用为某个 Action 而注册的每一个拦截器的 interecept 方法.

  3)每次调用 interecept 方法时, Struts 会传递一个 ActionInvocation 接口的实例.

  4)ActionInvocation: 代表一个给定 Action 的执行状态, 拦截器可以从该类的对象里获得与该 Action 相关联的 Action 对象和 Result 对象. 在完成拦截器自己的任务之后, 拦截器将调用 ActionInvocation 对象的 invoke 方法前进到 Action 处理流程的下一个环节

  5)AbstractInterceptor :类实现了 Interceptor 接口. 并为 init, destroy 提供了一个空白的实现

5.自定义拦截器

  1)定义一个拦截器的类

    > 可以实现Interceptor 接口

    > 继承AbstractInterceptor 抽象类 

  2)在struts.xml 文件配置

MyIntercepter.java
 1 package com.jason.intercepter;
 2 
 3 import org.eclipse.jdt.internal.compiler.ast.Invocation;
 4 
 5 import com.opensymphony.xwork2.ActionInvocation;
 6 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
 7 
 8 public class MyIntercepter extends AbstractInterceptor{
 9 
10     /**
11      *  @Fields:serialVersionUID
12      */
13     private static final long serialVersionUID = 1L;
14 
15     @Override
16     public String intercept(ActionInvocation invocation) throws Exception {
17         System.out.println(" before invovation.invoke ... ");
18         String result = invocation.invoke();
19         System.out.println(" after invovation.invoke ... ");
20         
21         return result;
22     }
23 
24 }

Struts.xml 配置自定义拦截器 和 使用拦截器

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<package name="default" namespace="/" extends="struts-default">
<interceptors>
         
           <interceptor name="hello" class="com.jason.intercepter.MyIntercepter"></interceptor>
</interceptors>
<action name="testToken" class="com.jason.upload.app.TokenAction">
             <!-- <interceptor-ref name="tokenSession"></interceptor-ref> -->
             
             <interceptor-ref name="hello"></interceptor-ref>
             
             <interceptor-ref name="token"></interceptor-ref>
             <interceptor-ref name="defaultStack"></interceptor-ref>
             <result>/success.jsp</result>
             <result name="invalid.token">/token-error.jsp</result>
         </action>
</package>
</package>

  3)注意:  

     自定义的拦截器中可以选择不用 ActionInvocation 的invoke() 方法。那么后序的拦截器和Action 方法不会被调用。Struts会渲染自定义拦截器 intercept方法返回值对应的result

 

原文地址:https://www.cnblogs.com/jasonHome/p/5928441.html