首页 > 代码库 > Struts2 interceptor使用经验小结
Struts2 interceptor使用经验小结
1. interceptor 调用Spring容器中的bean
在interceptor中常有需要调用Spring Bean的需要,其实很简单和Struts2的Action一样配置即可.
Spring中的配置
<!--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"/> 5 </interceptors>
2. 如何获得当前Action名字
public String intercept(ActionInvocation aInvocation) throws Exception { // 获取请求的action名称 String actionName = aInvocation.getInvocationContext().getName();
//获取参数集合
Map parameters = aInvocation.getInvocationContext().getParameters();
....}
3. 方法拦截器黑白名单可以使用通配符
拦截器代码:
package com.jd.jd_ptesting.interceptor;import com.jd.jd_ptesting.action.ProjectAction;import com.jd.jd_ptesting.action.SysDefectAction;import com.jd.jd_ptesting.po.Project;import com.jd.jd_ptesting.po.SysDefect;import com.jd.jd_ptesting.po.User;import com.jd.jd_ptesting.service.IProjectService;import com.jd.jd_ptesting.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<Project> 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<SysDefect> 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配置:
<bean id="authorityInterceptor" class="com.jd.jd_ptesting.interceptor.AuthorityInterceptor"/> <bean id="operationInterceptor" class="com.jd.jd_ptesting.interceptor.OperationInterceptor"> <property name="defectService" ref="sysDefectService"/> <property name="projectService" ref="projectService" />
<!-- 白名单属性配置,注意*的用法 --> <property name="includeMethods"> <value>*Modify,*Delete</value> </property> </bean>
struts2配置:
<interceptors> <interceptor name="loginInterceptor" class="authorityInterceptor"/> <interceptor name="operationInterceptor" class="operationInterceptor"/> <interceptor-stack name="authInterceptor-stack"> <interceptor-ref name="defaultStack"/> <interceptor-ref name="loginInterceptor"/> <interceptor-ref name="operationInterceptor"/> </interceptor-stack> </interceptors>
Struts2 interceptor使用经验小结
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。