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. CGRectGet *** 获取控件坐标的方法

    CGRectGetHeight返回label本身的高度 CGRectGetMinY返回label顶部的坐标 CGRectGetMaxY 返回label底部的坐标 CGRectGetMinX 返回lab ...

  2. crm 2013邮箱设置 “允许使用凭据进行电子邮件处理” 被禁用的解决

    记录一下: 在CRM 2013/2015的邮箱设置时发现“允许使用凭据进行电子邮件处理” 选项被禁用而且无法输入凭证(如下图): 查阅官方说明得知考虑邮件安全性只能在IFD部署或https访问模式下才 ...

  3. 利用docker compose启动gitlab及runner

    添加docker compose配置文件 新建文件docker-compose.yml,输入如下内容: gitlab: image: 'gitlab/gitlab-ce:latest' contain ...

  4. 336-Palindrome Pairs

    336-Palindrome Pairs Given a list of unique words, find all pairs of distinct indices (i, j) in the ...

  5. 史上最全的Python电子书教程资源下载(转)

    网上搜集的,点击即可下载,希望提供给有需要的人^_^   O'Reilly.Python.And.XML.pdf 2.02 MB   OReilly - Programming Python 2nd. ...

  6. 把HBITMAP 保存图片文件.

    //VC下把HBITMAP保存为bmp图片 BOOL CtestBitMapDlg::SaveBmp(HBITMAP hBitmap, CString FileName) { HDC hDC; //当 ...

  7. task9暂存

    <h6>第一组项目</h6> <div class="wrap"> <div class="title"> &l ...

  8. 一个的unity学习系列的博客

    1.http://my.csdn.net/caoboya 2.http://my.csdn.net/OnafioO

  9. ZooKeeper概述(转)

    译自http://zookeeper.apache.org/doc/trunk/zookeeperOver.html ZooKeeper是一个用于分布式应用的开源分布式协调服务.它提供了简单的原语集合 ...

  10. 深度学习(dropout)

    other_techniques_for_regularization 随手翻译,略作参考,禁止转载 www.cnblogs.com/santian/p/5457412.html Dropout: D ...