Struts2已经发布一段时间了,这个版本较struts1.x版本有了很大变化,其中一个就是增加了拦截器功能。这是个非常有用的功能,可是struts1.x却没有。     其实,struts1.x可以配合插件,实现拦截器的功能。     SAIF(Struts Action Invocation Framework)是一个开源组件,它让Struts框架具备Action拦截器与IOC的功能,这样你的1.x框架也就有了拦截器的功能。       1.将saif.jar包放入你的lib中。       2.创建Interceptor类。比如我在这里创建一个类:

package interceptor;
import java.io.IOException;
import javax.servlet.ServletException;
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse; 
import org.apache.struts.action.Action;
   import org.apache.struts.action.ActionForm;
   import org.apache.struts.action.ActionForward;
   import org.apache.struts.action.ActionMapping;
import net.sf.struts.saif.ActionHaveForwardInterceptor;
public class Display Interceptor implements ActionHaveForwardInterceptor{     
        public ActionForward afterAction(Action arg0, ActionMapping arg1,             
             ActionForm arg2, HttpServletRequest arg3, HttpServletResponse arg4)            
                 throws IOException, ServletException{         
                    // TODO Auto-generated method stub         
                            returnnull;     
                        }     
               public ActionForward beforeAction(Action action, ActionMapping mapping,              ActionForm form, HttpServletRequest request, HttpServletResponse response)             throws IOException, ServletException{         
            // TODO Auto-generated method stub         
                  System.out.println("Inteceptor...");        
                            if (!"fred".equals(request.getParameter("user_name"))){             
                                return mapping.findForward("noPermission");         
                                      }         
                                 returnnull;     
                                   } 
}

3.写interceptor配置文件:interceptor-config.xml。这个配置文件中指定了interceptor类和要被拦截的action

<?xml version="1.0" encoding="UTF-8"?>
<interceptor-config>   
<interceptorname="displayInterceptor" type="interceptor.DisplayInterceptor"/>        
<actiontype="/display">         
<interceptorname="displayInterceptor"/>   
</action>      </interceptor-config>

4.在struts-config.xml中指定加载interceptor-config.xml

<plug-in className="net.sf.struts.saif.SAIFSpringPlugin">     
<set-propertyproperty="interceptor-config" value="/WEB-INF/interceptor-config.xml"/>   
</plug-in>

ok,配置完后,启动服务器,然后输入.../display.do?user_name=fred,回车,这时候,这个请求就会被拦截来,

进入beforeAction中,进行验证,若验证成功,return null,就会转到action的forward指向的页面,若不成功,

就会转向另一个页面。

struts1拦截器的更多相关文章

  1. Struts2拦截器模拟

    前言: 接触Struts2已经有一段时间,Student核心内容就是通过拦截器对接Action,实现View层的控制跳转.本文根据自身理解对Struts2进行一个Java实例的模拟,方便大家理解! 示 ...

  2. 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s

     1. Spring-mvc介绍 1.1市面上流行的框架 Struts2(比较多) Springmvc(比较多而且属于上升的趋势) Struts1(即将被淘汰) 其他 1.2  spring-mv ...

  3. struts2 18拦截器详解(十)

    ModelDrivenInterceptor 该拦截器处于defaultStack中的第九的位置,在ScopedModelDrivenInterceptor拦截器之后,要使该拦截器有效的话,Actio ...

  4. ssh2——Interceptor拦截器

    尽管没学过struts1吧.可是了解到struts1中并没有拦截器,  到Struts2才有.它是基于WebWork发展起来的, 顾名思义,说到拦截器大家首先肯定会想到它是拦截东西的,起到一个限制的作 ...

  5. springMVC3.0(文件上传,@RequestMapping加參数,@SessionAttributes,@ModelAttribute,转发,重定向,数值获取,传參,ajax,拦截器)

    1.项目包结构例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsiz ...

  6. 6. ModelDriven拦截器、Preparable 拦截器

    1. 问题 Struts2 的 Action 我们将它定义为一个控制器,但是由于在 Action 中也可以来编写一些业务逻辑,也有人会在 Action 输入业务逻辑层. 但是在企业开发中,我们一般会将 ...

  7. springmvc的拦截器

    什么是拦截器                                                         java里的拦截器是动态拦截action调用的对象.它提供了一种机制可以使 ...

  8. Struts的拦截器

    Struts的拦截器 1.什么是拦截器 Struts的拦截器和Servlet过滤器类似,在执行Action的execute方法之前,Struts会首先执行Struts.xml中引用的拦截器,在执行完所 ...

  9. Struts2拦截器的执行过程浅析

    在学习Struts2的过程中对拦截器和动作类的执行过程一度陷入误区,特别读了一下Struts2的源码,将自己的收获分享给正在困惑的童鞋... 开始先上图: 从Struts2的图可以看出当浏览器发出请求 ...

随机推荐

  1. mssql2000 身份证号码验证

    CREATE VIEW thisDate --返回当前日期,因为自定义函数中不能使用GETDATE() AS ),) AS aDate )) /*mssql2000 返回值=0,身份证校验正确 1:位 ...

  2. 实现浏览器打开图片的url默认是下载

    在返回的response里面设置HTTP头的Content-Disposition=attachement;filename=xxxx,即可实现文件另存为"xxxx":Conten ...

  3. python install_opener用法

    opener:当你获取一个URL时,你使用一个opener(OpenerDirector).正常情况下我们一直使用默认的opener,通过urlopen,但你也可以创建自定义的openers. url ...

  4. 循序渐进Python3(十一) --3--  web之dom

    DOM                  文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的 ...

  5. PO、VO、BO、DTO、POJO、DAO

    J2EE开发中大量的专业缩略语很是让人迷惑,尤其是跟一些高手讨论问题的时候,三分钟就被人家满口的专业术语喷晕了,PO VO BO DTO POJO DAO,一大堆的就来了(听过老罗对这种现象的批判的朋 ...

  6. C++设计模式——简单工厂模式

    简单工厂模式(Simple Factory Pattern) 介绍:简单工厂模式不能说是一个设计模式,说它是一种编程习惯可能更恰当些.因为它至少不是Gof23种设计模式之一.但它在实际的编程中经常被用 ...

  7. C#实现JSON序列化与反序列化

    1.使用 JavaScriptSerializer类实现序列化 namespace: System.Web.Script.Serialization eg: // 序列化 private string ...

  8. 关于rem的计算顺序

    /*响应式字体*//* * 字体响应式 * 屏幕>640px时,html字体大小 * 屏幕<640px时,html字体根据屏幕做出相应  * */(function(doc,win){   ...

  9. 小程序de 一些经验1

    尝试着写微信的小程序,一个简单的表单验证.一开始就花了大把的时间尝试如何开始小程序的准备工作. 鼓捣半天,AppId是没有的,于是用了不用appId的模拟版.其实只要下载一个小程序版的微信开发工具. ...

  10. 實際案例: 獲取臨時票証 (JsApi Ticket)

    專案中選用大名鼎鼎的 Senparc 微信開發套件 獲取臨時票證處理常式的程式碼 (GetgVXinInfo.ashx) using Senparc.Weixin; using Senparc.Wei ...