博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Struts2 interceptor使用经验小结
阅读量:6940 次
发布时间:2019-06-27

本文共 4426 字,大约阅读时间需要 14 分钟。

1. interceptor 调用Spring容器中的bean

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

Spring中的配置

1
2 3
4
5
6
7
*Modify,*Delete
8
9

Struts2中的配置

1      
2
3
5

2. 如何获得当前Action名字  

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

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

    拦截器代码:

  

package xx.interceptor;import xx.action.ProjectAction;import xx.action.SysDefectAction;import xx.po.Project;import xx.po.SysDefect;import xx.po.User;import xx.service.IProjectService;import xx.service.ISysDefectService;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;import java.util.List;public class OperationInterceptor extends MethodFilterInterceptor {    private ISysDefectService defectService;    private IProjectService projectService;      protected String doIntercept(ActionInvocation actionInvocation) throws Exception {        boolean hasAuth = false;        String cunrrentId = "";        Object currentActon =actionInvocation.getAction();        ActionContext actionContext = actionInvocation.getInvocationContext();        User currentUser = (User)actionContext.getSession().get("currentUser");        if(currentUser == null                || currentUser.getEmployer()==null                || currentUser.getGroup() == null                || currentUser.getEmployer().getEmpId()+""==null        )        {            return Action.LOGIN;        }        //获取当前用户的epmId        String empId = currentUser.getEmployer().getEmpId() + "";        if(currentActon instanceof ProjectAction){            //是否第二次检查权限            List
projectList = currentUser.getProjectList(); if (projectList==null || projectList.size()<1){ ProjectAction projectAction = (ProjectAction)currentActon; cunrrentId = projectAction.getProjId(); projectList = projectService.getProjectsByEmpId(empId); } //如果获取列表失败,则提示无权限 if(projectList==null || projectList.size()<1){ return "deny"; }else { currentUser.setProjectList(projectList); } for(Project project:projectList){ if(cunrrentId.equals(project.getProjId()+"")){ hasAuth = true; } } if(hasAuth){ return actionInvocation.invoke(); } }else if(currentActon instanceof SysDefectAction){ SysDefectAction sysDefectAction = (SysDefectAction)currentActon; List
sysDefectList = defectService.getSysDefectsByEmpId(empId); if(sysDefectList==null || sysDefectList.size()<1){ return "deny"; }else { currentUser.setSysDefectList(sysDefectList); } for(SysDefect sysDefect:sysDefectList){ if(cunrrentId.equals(sysDefect.getDefId()+"")){ hasAuth = true; } } if(hasAuth){ return actionInvocation.invoke(); } } return "deny"; //To change body of implemented methods use File | Settings | File Templates. } public ISysDefectService getDefectService() { return defectService; } public void setDefectService(ISysDefectService defectService) { this.defectService = defectService; } public IProjectService getProjectService() { return projectService; } public void setProjectService(IProjectService projectService) { this.projectService = projectService; }}

  Spring配置:    

     
*Modify,*Delete

 

  struts2配置:

 

 

 

  

转载于:https://www.cnblogs.com/onmyway20xx/p/4014199.html

你可能感兴趣的文章
LeetCode(7): Majority Element
查看>>
一个完整的大作业
查看>>
软考之操作系统
查看>>
【C015】Python数据类型 - 序列
查看>>
简单之美-软件开发实践者的思考 02
查看>>
关于QueryCache的一次打脸
查看>>
SQL Server 与 Windows 内存使用上的约定
查看>>
thinkphp5部署在宝塔面板问题!
查看>>
MATLAB绘图功能(1) 二维高层绘图操作
查看>>
js验证身份证id
查看>>
python:Json模块dumps、loads、dump、load介绍
查看>>
【JavaScript学习笔记】点击消失
查看>>
DAY13 Matlab直角坐标系实现图像旋转
查看>>
3D酷炫翻牌效果
查看>>
HDU3466Proud Merchants(贪心&背包)
查看>>
nginx 用来做什么?
查看>>
js基础内容 原型与实例
查看>>
Linux netstat命令详解(转)
查看>>
五月13号冲刺
查看>>
常用正则表达式
查看>>