美文网首页
Struts2自定义拦截器

Struts2自定义拦截器

作者: 小山雀 | 来源:发表于2017-10-30 15:57 被阅读0次

概述

本文简单介绍如何自定义一个Struts拦截器,但不涉及拦截器基础、原理等其他知识,仅仅只是介绍自定义拦截器的步骤。本人目前的能力只能如此。我是学习Struts2时写的这段文字,请带着怀疑的态度阅读。

详细步骤

创建一个Struts2项目

创建一个HelloWorld项目,上一篇已经介绍过了,不赘述。

创建一个拦截器类

创建拦截器类必须要实现一个Interceptor 接口:

public interface Interceptor extends Serializable{
   void destroy();
   void init();
   String intercept(ActionInvocation invocation)
   throws Exception;
}

该接口中有3个方法:init(),intercept(),destroy()。init方法是初始化拦截器方法;destroy方法是拦截器清理方法;intercept 是拦截器方法,是实现拦截器功能的主要方法。

本例中拦截器继承AbstractInterceptor,使用AbstractInterceptor默认的init和destroy方法,创建MyInterceptor类:

package com.xxx.action;

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

public class MyInterceptor extends AbstractInterceptor{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
          /* let us do some pre-processing */
          String output = "Pre-Processing"; 
          System.out.println(output);

          /* let us call action or next interceptor */
          String result = invocation.invoke();

          /* let us do some post-processing */
          output = "Post-Processing"; 
          System.out.println(output);

          return result;
    }

}

简单的解释一下,String result = invocation.invoke();这行的作用是:如果还有其他拦截器,则调用其他拦截器,如果没有,那么就调用action方法。也就是说,在 invocation.invoke();之前是在调用action之前做的预处理,在 invocation.invoke();之后是对调用action后的结果的处理。

配置拦截器

在strtuts.xml中注册拦截器:

 <interceptors>
        <interceptor name="myinterceptor" class="com.xxx.action.MyInterceptor"/>   
</interceptors>

name是拦截器名,是引用拦截器的标志,class是拦截器的实现类的路径。

在<action>标签下为action配置拦截器:

 <action name="hello" 
            class="com.xxx.action.HelloWorldAction">
            
             <result name="success">/HelloWorld.jsp</result>
            <result name="error">/Erro.jsp</result>
            
            <interceptor-ref name="params"></interceptor-ref>
            <interceptor-ref name="myinterceptor"></interceptor-ref>
            
            
</action>

在<interceptor-ref name="myinterceptor"></interceptor-ref>中填写正确的拦截器名。

部署到服务器运行测试

在服务器中运行即可,最终的项目结构如下:

Paste_Image.png

相关文章

  • struts2 - 创建包级别的拦截器

    struts2的拦截器是核心,下面是自定义的拦截器, 自定义的拦截器需要继承AbstractInterceptor

  • struts2 自定义拦截器

    一、为什么要自定义拦截器 在struts2里面有很多拦截器,这些拦截器是struts2封装的功能,但是在实际开发中...

  • Struts2拦截器登录验证

    Struts2拦截器 Struts2拦截器的概念和Spring Mvc拦截器一样。 Struts2拦截器是在访问某...

  • Struts2拦截器

    自定义拦截器: 从struts2的apps中拷贝相应jar包 然后建struts2项目 加入到buildpath ...

  • struts2 拦截器介绍

    1.Interceptor介绍 拦截器(Intercepter):拦截器是struts2的核心,struts2的众...

  • struts2实验5:struts2 拦截器

    layout: post title: struts2实验5:struts2 拦截器 categories: S...

  • struts2自定义拦截器

    题目:使用struts2自定义拦截器,完成用户登陆才能访问权限的实现 在session中存放user变量表示用户登...

  • Struts2学习笔记(第三天)

    国际化 拦截器(interceptor) struts2中怎样使用拦截器 分析拦截器原理 关于intercepto...

  • 拦截器

    拦截器的概述: 1、struts2是框架,封装了很多的功能,struts2里面封装的功能都是在拦截器里面。2、st...

  • SSH框架之Struts2的拦截器(四)

    第一节:Struts2的拦截器 1.1 拦截器的概述: 拦截器,在AOP(Aspect - Oriented Pr...

网友评论

      本文标题:Struts2自定义拦截器

      本文链接:https://www.haomeiwen.com/subject/xbdlwttx.html