对于需要登陆验证、权限验证等功能的网站,每一次请求,每一个action都写一段验证的代码,未免显得冗余且不易维护。struts2提供了拦截器interceptor,为这些页面提供一个切面,或者说公共组件,以达到易维护、提高代码重用率的目的。struts默认的interceptor有很多,就不去一一列举,这里从自定义拦截器开始学习。

1、定义一个自定义拦截器:

package com.owlforest.home.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class MyInterceptor extends AbstractInterceptor {
    public String intercept(ActionInvocation invocation) throws Exception {
        String output = "Pre-Processing";
        System.out.println(output);

        String result = invocation.invoke();

        output = "Post-Processing";
        System.out.println(output);

        return result;
    }
}

2、修改struts.xml,在package中声明和使用拦截器MyInterceptor

<package name="suibian" extends="struts-default">
        <!--声明自定义的拦截器-->
        <interceptors>
            <interceptor name="myinterceptor"
                         class="com.owlforest.home.interceptor.MyInterceptor" />
        </interceptors>
        <action name="hello" class="com.owlforest.home.action.HelloWorldAction"
                method="excute">
            <!--使用自定义拦截器-->
            <interceptor-ref name="myinterceptor" />
            <interceptor-ref name="params"/>
            <result name="success">/HelloWorld.jsp</result>
        </action>
    </package>

该拦截器在com.owlforest.home.action.HelloWorldAction执行前后会生效,为了便于理解,附上该Action的代码:

package com.owlforest.home.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {
    private String name;

    public String excute(){
        System.out.println("excute");
        return "success";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

3、执行程序

程序实现参照Struts2学习:HelloWorld

执行action过后可查看控制台输出,看到下面的信息后及说明生效。

4、对于一个struts.xml里面的package,可能有多个action,多个action可能会用到一系列的相同的interceptor,如果每个action都要写一系列相同的拦截器,则很难维护。struts2提供了拦截器栈,来帮助整合这些拦截器,达到复用的目的。

为了方便理解,我写了两个自定义的拦截器MyInterceptor和SecInterceptor

package com.owlforest.home.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class MyInterceptor extends AbstractInterceptor {
    public String intercept(ActionInvocation invocation) throws Exception {
        String output = "Pre-Processing  拦截器MyInterceptor";
        System.out.println(output);

        String result = invocation.invoke();

        output = "Post-Processing  拦截器MyInterceptor";
        System.out.println(output);

        return result;
    }
}
package com.owlforest.home.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class SecInterceptor extends AbstractInterceptor {
    public String intercept(ActionInvocation invocation) throws Exception {
        String output = "Pre-Processing  SecInterceptor";
        System.out.println(output);

        String result = invocation.invoke();

        output = "Post-Processing  SecInterceptor";
        System.out.println(output);

        return result;
    }
}

配置struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <!-- 设置struts是否为开发模式,默认为false,测试阶段一般设为true. -->
    <constant name="struts.devMode" value="true"/>
    <package name="suibian" extends="struts-default">
        <interceptors>
            <interceptor name="myinterceptor"
                         class="com.owlforest.home.interceptor.MyInterceptor" />
            <interceptor name="secinterceptor"
                         class="com.owlforest.home.interceptor.SecInterceptor" />
            <interceptor-stack name="myinterceptorstack">
                <interceptor-ref name="myinterceptor" />
                <interceptor-ref name="secinterceptor" />
            </interceptor-stack>
        </interceptors>
        <action name="hello" class="com.owlforest.home.action.HelloWorldAction"
                method="excute">
            <interceptor-ref name="myinterceptorstack"/>
            <interceptor-ref name="params"/>
            <result name="success">/HelloWorld.jsp</result>
        </action>
    </package>
</struts>

5、因为拦截器和拦截器栈是在package中声明使用的,作用域仅在当前声明的package中,对于一些验证、异常相关的拦截器,需要在全局、也就是所有的package中都能使用,这种需要定义一个全局的package,使相应的action对应的package继承自该全局package即可。如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <!-- 设置struts是否为开发模式,默认为false,测试阶段一般设为true. -->
    <constant name="struts.devMode" value="true"/>
    <!--全局的package-->
    <package name="commominterceptor" extends="struts-default">
        <interceptors>
            <interceptor name="myinterceptor"
                         class="com.owlforest.home.interceptor.MyInterceptor" />
            <interceptor name="secinterceptor"
                         class="com.owlforest.home.interceptor.SecInterceptor" />
            <interceptor-stack name="myinterceptorstack">
                <interceptor-ref name="myinterceptor" />
                <interceptor-ref name="secinterceptor" />
            </interceptor-stack>
        </interceptors>
    </package>
    <!--继承自commominterceptor的package-->
    <package name="suibian" extends="commominterceptor">
        <action name="hello" class="com.owlforest.home.action.HelloWorldAction"
                method="excute">
            <interceptor-ref name="myinterceptorstack"/>
            <interceptor-ref name="params"/>
            <result name="success">/HelloWorld.jsp</result>
        </action>
    </package>
</struts>

拦截器栈与全局拦截器的运行效果如下:

Struts2学习:interceptor(拦截器)的使用的更多相关文章

  1. 模仿Struts2的Interceptor拦截器实现

    模仿Struts2的Interceptor拦截器实现 public interface Invocation { public Object invoke(); } public interface ...

  2. struts2学习笔记--拦截器(Interceptor)和登录权限验证Demo

    理解 Interceptor拦截器类似于我们学过的过滤器,是可以在action执行前后执行的代码.是我们做web开发是经常使用的技术,比如权限控制,日志.我们也可以把多个interceptor连在一起 ...

  3. struts2学习(5)拦截器简介以及例子执行过程

    一.拦截器简介: 二.Struts2预定义拦截器&拦截器栈 在执行action之前和之后,拦截器进行了操作: 比如struts-default.xml中就有很多预定义的拦截器:   拦截器栈: ...

  4. Struts2学习之拦截器

    © 版权声明:本文为博主原创文章,转载请注明出处 拦截器: - Struts2大多数核心功能都是通过拦截器实现的,每个拦截器完成某项功能 - 拦截器方法在Action执行之前或之后执行 工作原理: - ...

  5. struts2自定义Interceptor拦截器

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  6. Struts2学习之拦截器栈

    © 版权声明:本文为博主原创文章,转载请注明出处 拦截器栈: - 从结构上看:拦截器栈相当于多个拦截器的组合 - 从功能上看:拦截器栈也是拦截器 默认拦截器栈: - 在struts-core.jar中 ...

  7. Struts2基础学习(五)—拦截器

    一.概述 1.初识拦截器      Interceptor 拦截器类似前面学过的过滤器,是可以在action执行前后执行的代码,是我们做Web开发经常用到的技术.比如:权限控制.日志等.我们也可以将多 ...

  8. 1.5(Spring MVC学习笔记) 拦截器(Interceptor)

    一.拦截器 1.1拦截器简介 Spring中的拦截器类似Servlet中的过滤器(Filter),主要用于拦截用户请求, 并进行一定的处理操作(如验证权限.记录日志.设置编码方式等). 1.2拦截器实 ...

  9. 5.Struts2中的拦截器

    拦截器是Struts2中的核心,其自带很多很多的拦截器,这里主要介绍一下自定义拦截器,恩多一半情况下呢?我们不需要使用到自定义的拦截器,Struts2本身已经提 供了很多的拦截器供我们使用,对于自定义 ...

  10. SpringMVC中使用Interceptor拦截器

    SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...

随机推荐

  1. cachecloud安装部署

    项目地址:https://github.com/sohutv/cachecloud # 初始化数据库 默认插入admin超级管理员,用户名admin, 密码:admin 安装mysql yum -y ...

  2. yii 分页查询

    控制器 <?php namespace backend\controllers; use app\models\Comment; use app\models\Commentstatus; us ...

  3. Spring Cloud(Dalston.SR5)--Ribbon 中间层负载均衡

    Spring Cloud 集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,使用 @LoadBalanced 修饰的 RestTemplate 类拥有了负载均衡功能,在 Sprin ...

  4. 使用php生成数字、字母组合验证码

    项目中经常会遇到一些登陆验证,支付验证等等一系列安全验证的策略.实现方法多种多样,下面就来讲解下如何用php生成简单的文字+数字组合的验证码: 所用语言php,gd库 原理解释: a>实质上是在 ...

  5. php+google/baidu翻译接口

    <?php /** * @link http://www.joinf.com * @copyright Copyright (C) 2017 joinf.com. All rights rese ...

  6. ML: 聚类算法R包-网格聚类

    网格聚类算法 optpart::clique optpart::clique CLIQUE(Clustering In QUEst)是一种简单的基于网格的聚类方法,用于发现子空间中基于密度的簇.CLI ...

  7. C#、AE开发入门之打开TIFF文件并显示

    继上篇文章,本次打开TIFF文件,附上源码及其注释 private void button2_Click(object sender, EventArgs e) { axMapControl1.Cle ...

  8. 服务网关zuul之四:zuul网关配置

    禁用过滤器在Zuul中特别提供了一个参数来禁用指定的过滤器,该参数的配置格式如下:zuul.AccessFilter.pre.disable=true动态加载动态路由通过结合Spring Cloud ...

  9. Oracle 非归档--归档操作流程

    转载自链接  https://blog.csdn.net/u013611461/article/details/53558077 SQL> shutdown immediate; Databas ...

  10. [UE4]虚幻引擎UE4如何制作可拖动(Drag and Drop)的背包(Scrollbox)(转载)

    最终效果 由于隐私保护,不想截实际的效果图,下面给出了示意图,左边是背包A,右边是背包B,将其中的子项目从左侧拖往右侧的背包,然后在插入位置放置. 第一步: 制作一个user widget(在内容浏览 ...