1. interceptor 调用Spring容器中的bean

在interceptor中常有需要调用Spring Bean的需要,其实很简单和Struts2的Action一样配置即可.

Spring中的配置

  1. <!--spring配置 -->
    1 <bean id="authorityInterceptor" class="com.xxx.interceptor.AuthorityInterceptor"/>
  2.  
  3. <bean id="operationInterceptor" class="com.xxx.interceptor.OperationInterceptor">
  4. <property name="defectService" ref="sysDefectService"/>
  5. <property name="projectService" ref="projectService" />
  6. <property name="includeMethods">
  7. <value>*Modify,*Delete</value>
  8. </property>
  9. </bean>

Struts2中的配置

  1. <interceptors>
  2. <interceptor name="loginInterceptor" class="authorityInterceptor"/>
  3. <interceptor name="operationInterceptor" class="operationInterceptor"/>
  4. </interceptors>

2. 如何获得当前Action名字  

  1. public String intercept(ActionInvocation aInvocation) throws Exception {
  2.   // 获取请求的action名称
  3.   String actionName = aInvocation.getInvocationContext().getName();
  1.   
      //获取参数集合
      Map parameters = aInvocation.getInvocationContext().getParameters();
  1.    ....
  2. }

3. 方法拦截器黑白名单可以使用通配符

拦截器代码:

  

  1. package xx.interceptor;
  2. import xx.action.ProjectAction;
  3. import xx.action.SysDefectAction;
  4. import xx.po.Project;
  5. import xx.po.SysDefect;
  6. import xx.po.User;
  7. import xx.service.IProjectService;
  8. import xx.service.ISysDefectService;
  9. import com.opensymphony.xwork2.Action;
  10. import com.opensymphony.xwork2.ActionContext;
  11. import com.opensymphony.xwork2.ActionInvocation;
  12. import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
  13. import java.util.List;
  14.  
  15. public class OperationInterceptor extends MethodFilterInterceptor {
  16. private ISysDefectService defectService;
  17. private IProjectService projectService;
  18.  
  19. protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
  20. boolean hasAuth = false;
  21. String cunrrentId = "";
  22. Object currentActon =actionInvocation.getAction();
  23. ActionContext actionContext = actionInvocation.getInvocationContext();
  24. User currentUser = (User)actionContext.getSession().get("currentUser");
  25. if(currentUser == null
  26. || currentUser.getEmployer()==null
  27. || currentUser.getGroup() == null
  28. || currentUser.getEmployer().getEmpId()+""==null
  29. )
  30. {
  31. return Action.LOGIN;
  32. }
  33. //获取当前用户的epmId
  34. String empId = currentUser.getEmployer().getEmpId() + "";
  35.  
  36. if(currentActon instanceof ProjectAction){
  37. //是否第二次检查权限
  38. List<Project> projectList = currentUser.getProjectList();
  39. if (projectList==null || projectList.size()<1){
  40. ProjectAction projectAction = (ProjectAction)currentActon;
  41. cunrrentId = projectAction.getProjId();
  42. projectList = projectService.getProjectsByEmpId(empId);
  43. }
  44.  
  45. //如果获取列表失败,则提示无权限
  46. if(projectList==null || projectList.size()<1){
  47. return "deny";
  48. }else {
  49. currentUser.setProjectList(projectList);
  50. }
  51. for(Project project:projectList){
  52. if(cunrrentId.equals(project.getProjId()+"")){
  53. hasAuth = true;
  54. }
  55. }
  56. if(hasAuth){
  57. return actionInvocation.invoke();
  58. }
  59.  
  60. }else if(currentActon instanceof SysDefectAction){
  61. SysDefectAction sysDefectAction = (SysDefectAction)currentActon;
  62. List<SysDefect> sysDefectList = defectService.getSysDefectsByEmpId(empId);
  63. if(sysDefectList==null || sysDefectList.size()<1){
  64. return "deny";
  65. }else {
  66. currentUser.setSysDefectList(sysDefectList);
  67. }
  68. for(SysDefect sysDefect:sysDefectList){
  69. if(cunrrentId.equals(sysDefect.getDefId()+"")){
  70. hasAuth = true;
    }
  71. }
  72. if(hasAuth){
  73. return actionInvocation.invoke();
  74. }
  75. }
  76. return "deny"; //To change body of implemented methods use File | Settings | File Templates.
  77. }
  78.  
  79. public ISysDefectService getDefectService() {
  80. return defectService;
  81. }
  82.  
  83. public void setDefectService(ISysDefectService defectService) {
  84. this.defectService = defectService;
  85. }
  86.  
  87. public IProjectService getProjectService() {
  88. return projectService;
  89. }
  90.  
  91. public void setProjectService(IProjectService projectService) {
  92. this.projectService = projectService;
  93. }
  94. }

  Spring配置:

  1. <bean id="authorityInterceptor" class="xx.interceptor.AuthorityInterceptor"/>
  2.  
  3. <bean id="operationInterceptor" class="xx.interceptor.OperationInterceptor">
  4. <property name="defectService" ref="sysDefectService"/>
  5. <property name="projectService" ref="projectService" />
  1.      <!-- 白名单属性配置,注意*的用法 -->
  2. <property name="includeMethods">
  3. <value>*Modify,*Delete</value>
  4. </property>
  5. </bean>

  struts2配置:

  1. <interceptors>
  2. <interceptor name="loginInterceptor" class="authorityInterceptor"/>
  3. <interceptor name="operationInterceptor" class="operationInterceptor"/>
  4.  
  5. <interceptor-stack name="authInterceptor-stack">
  6. <interceptor-ref name="defaultStack"/>
  7. <interceptor-ref name="loginInterceptor"/>
  8. <interceptor-ref name="operationInterceptor"/>
  9.  
  10. </interceptor-stack>
  11.  
  12. </interceptors>

Struts2 interceptor使用经验小结的更多相关文章

  1. Struts2 Interceptor学习

    Interceptor的设计思想,其实是Spring里面的AOP思想,尽管Struts2又有自己的Interceptor但是,在实际开发中,用的较少,SSH整合之后你可以采用AOP事务处理进行拦截,更 ...

  2. Struts2 - Interceptor中取得ActionName、Namespace、Method

    在Struts2的Interceptor中取得当前执行对应的ActionName.Namespace.Method方法: 可以使用: System.out.println(invocation.get ...

  3. Struts2命令空间小结

    sturts2命名空间小结,以tomcat为服务器 1. 命名空间配置为“/” <package name="default" namespace="/" ...

  4. 【心得】Lattice后端使用经验小结(ECP5UM,DDR3,Diamond3.10,Reveal逻辑分析)

    [博客导航] [导航]FPGA相关 背景 下边的内容,适合初次使用Lattice的.具备FPGA开发经验的同学. 1.初次使用,还真遇到不少的坑,Lattice的工具也有不少优缺点,通过总结,希望能缩 ...

  5. Struts2(七)基础小结

    一.struts2和action 二.Result 三.struts.xml 四.namespace 第一种绝对路径 <form action="${pageContext.reque ...

  6. Git使用经验小结

    2012春,开始正式使用SVN,2014年9月加入一起好后,开始使用Git.  有了一些使用经验,也看了下网上关于"Git全胜SVN"的言论. 结合自己的实际情况,简要写几句: 1 ...

  7. mybatis 使用经验小结

    一.多数据源问题 主要思路是把dataSource.sqlSesstionFactory.MapperScannerConfigurer在配置中区分开,各Mapper对应的包名.类名区分开 <? ...

  8. jmeter linux使用经验小结

    1. 确认务必安装了JDK,并且把路径配置OK.否则执行会报错. 2. 当做负载机时,在hosts 配置上    你的ip   你的hostname  或者使用./bin/jmeter-server ...

  9. DEV_TreeList使用经验小结

    1. 点击叶子节点是希望Open键显示,点击非叶子节点时希望隐藏.实践中发现点击到了非叶子节点图标,Open没有隐藏,如何解决? 增加一个判断: if (_hitInfo.HitInfoType != ...

随机推荐

  1. 通过样式调整input 中password text默认长度

    原文出处 <input >标签内的type分别为password和text时其默认长度和宽度不一致,而在做登陆框时往往需要将它们的长度和宽度设置一致.如下的方法可以通过css控制使其一致: ...

  2. oracle数据库不支持mysql中limit功能

    oracle数据库不支持mysql中limit功能,但可以通过rownum来限制返回的结果集的行数,rownum并不是用户添加的字段,而是oracle系统自动添加的. (1)使查询结果最多返回前10行 ...

  3. EasyUIDataGrid 的List<T>转Json

    EasyUI的DataGrid的Json自己拼接的话非常麻烦,而且容易出错,于是写了个通用的方法! CustomList<T>自定义类,继承于List<T>,用来处理返回的实体 ...

  4. 关于ServletConfig的小结

         在Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数.当servlet配置了初始化参数后,web容器在创建servlet ...

  5. IIS7 发现无法显示ewebeditor编辑器成空白

    vs2003写的网站,很早了,编辑器用的是ewebeditor,每次更换程序编辑器都会出问题.今天记录一下. 内部老网站在Windows2003 iis6上运行的. 现在要迁移到2008上64位.08 ...

  6. Headfirst设计模式的C++实现——迭代器(Iterator)改良版

    iterator.h #ifndef _ITERATOR_H_ #define _ITERATOR_H_ class Iterator { public: ; ; }; #endif menu.h # ...

  7. hdu 4850 Wow! Such String! 欧拉回路

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4080264.html 题目链接:hdu 4850 Wow! Such String! 欧拉回 ...

  8. new static() 和 new self() 的区别异同

    长夜漫漫啊! 今天领导本地搭建一个站.发现用PHP 5.2 搭建不起来,站PHP代码里面有很多5.3以上的部分,领导让苦逼我更改在5.2下能运行. 改着改着发现了一个地方 return new sta ...

  9. csdn博客刷点击率代码

    此文为转载,亲测有效. import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; impo ...

  10. C# Activex开发、打包、签名、发布

    一.前言      最近有这样一个需求,需要在网页上面启动客户端的软件,软件之间的通信.调用,单单依靠HTML是无法实现了,因此必须借用Activex来实现.由于本人主要擅长C#,自然本文给出了用C# ...