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指向的页面,若不成功,

就会转向另一个页面。

最新文章

  1. CSS 特殊属性介绍之 pointer-events
  2. Spring Enable annotation – writing a custom Enable annotation
  3. 十分钟轻松让你认识ASP.NET MVC6
  4. li标签的点击范围
  5. JAVA-集合作业-已知有十六支男子足球队参加2008 北京奥运会。写一个程序,把这16 支球队随机分为4 个组。采用List集合和随机数
  6. MACD、BOLL、KDJ 三大组合精准把握趋势与买卖!
  7. SQL Server中追踪器Trace的介绍和简单使用
  8. C/C++笔试经典程序(二)
  9. android 5.0开发环境搭建
  10. 初识PHP遗留下来的问题?
  11. 2017-12-20python全栈9期第五天第二节之字典的增删查改和字典的for循环
  12. 容器云技术选择之kubernetes和swarm对比
  13. iOS imageNamed 与 imageWithContentsOfFile 的区别
  14. java连接数据库报了ssl连接的警告
  15. MUI学习02-顶部导航栏
  16. 创建多线程的第一种方式——创建Thread子类和重写run方法
  17. luogu 1631 序列合并
  18. tex---就是tex文件,这个地球人都知道,是文章所在的主要文件
  19. 二叉树最大宽度 Maximum Width of Binary Tree
  20. (数据科学学习手札55)利用ggthemr来美化ggplot2图像

热门文章

  1. Web前端开发:为何选择MVVM而非MVC
  2. 15 个有趣的 JavaScript 与 CSS 库
  3. node npm 安装模块 淘宝镜像
  4. ORACLE临时表空间
  5. 介绍开源的.net通信框架NetworkComms框架 源码分析(二十二 )TCPConnectionStatic
  6. pip install lxml出错解决
  7. android添加第三方字体并设置的简单使用
  8. hdu 3307 Description has only two Sentences (欧拉函数+快速幂)
  9. 产生某个区间的随机整数 int #Java
  10. Java swing项目-图书管理系统(swing+mysql+jdbc) 总结