• Struts2


    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

  • 相关阅读:
    WPF线程调用UI元素
    WPF使用阿里巴巴矢量图标图ttf
    WPF读取Excel表格数据到DataGrid
    WPF开发的软件在触摸屏当操作遇到边界时
    WPF 全局样式(滚动条)
    WPF TextBox文本自动换行
    Maven : Cannot download sources
    nginx关闭后仍然能够访问localhost:80
    微信公众号开发总结
    CAT window10 本地安装
  • 原文地址:https://www.cnblogs.com/pjlhf/p/8722413.html
Copyright © 2020-2023  润新知