1. 自定义的拦截器
第一步:编写自定义的拦截器类,该类实现import
com.opensymphony.xwork2.interceptor.Interceptor;接口
//下面模拟用户登录
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class PermissionInterceptor implements Interceptor {
public void destroy() {
System.out.println("======PermissionInterceptor=====destroy============");
}
public void init() {
System.out.println("====PermissionInterceptor===init================");
}
public String intercept(ActionInvocation invocation) throws Exception {
Object user = ActionContext.getContext().getSession().get("user");
if(user!=null) return invocation.invoke(); //如果user不为null,代表用户已经登录,允许执行action中的方法
ActionContext.getContext().put("message", "你没有权限执行该操作");
return "success";
}
}
第二步:在struts.xml中配置拦截器
<struts>
<package name="employee" namespace="/control/employee" extends="struts-default">
<interceptors> //配置拦截器组
<interceptor name="permission" class="cn.itcast.interceptor.PermissionInterceptor"/>
<interceptor-stack name="permissionStack"> //配置拦截器栈
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="permission" />
</interceptor-stack>
</interceptors>
<global-results>
<result name="success">/WEB-INF/page/message.jsp</result>
</global-results>
<action name="list_*" class="cn.itcast.action.HelloWorldAction" method="{1}">
<interceptor-ref name="permissionStack" /> //把拦截器添加到具体的某个action中
</action>
</package>
</struts>
注意:在action上添加拦截器时,action本身有一系列的struts2的拦截器,所以要先保留action本身的拦截器,
再添加自定义的拦截器,具体做法,就是定义一个拦截器栈interceptor-stack,在栈中先保留defaultStack拦截器
再添加自定义的拦截器,且要把defaultStack拦截器放在最前面,应为是按执行顺序来调用拦截器的
分享到:
相关推荐
简单的拦截器例子,适合初学者学习,也是一个备忘录
Struts2拦截器(Interceptor) Struts2拦截器(Interceptor)
struts2 拦截器_教程.zip
STRUTS2:拦截器Interceptor
struts2 Interceptor拦截器 http://whitewolf.0fees.net
struts2,Interceptor struts2拦截器实例,两套实例,一套是针对单个的action配置的,另一套实例是针对全局的action配置的拦截器interceptor
拦截器是一种AOP(面向切面编程)思想的编程方式.它提供一种机制是开发者能够把相对独立的代码抽离出来,...下面这篇文章主要给大家介绍了关于Struts2拦截器Interceptor的原理与配置的相关资料,需要的朋友可以参考下。
Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现.
Struts2规定用户自定义拦截器必须实现com.opensymphony.xwork2.interceptor.Interceptor接口。该接口声明了3个方法, void init(); void destroy(); String intercept(ActionInvocation invocation) throws ...
Struts2.3.6实现自定义拦截器Interceptor http://blog.csdn.net/alanchen520/article/details/34086699
创建自己struts2拦截器,手工创建自己的拦截器实现
综合使用servlet filter与struts2 interceptor对权限进行验证。
NULL 博文链接:https://johnson2132.iteye.com/blog/720398
使用Struts2实现用户权限拦截、重点是掌握拦截器的使用和配置方法,通过案例的学习,就能掌握struts中拦截器的使用方法,让我们能快速上手
Struts2 拦截器详细配置过程 1:所有拦截器的超级接口Interceptor ,Action去实现这个接口; Interceptor 它其中有三个方法(init(),destroy() ,interceptor()): Init()方法:在服务器起动的时候加载一次,并且只加载一...
Interceptor拦截器详解和部分源码的指导,方便深入学习struts2,增加理解
拦截器可以说相当于是个过滤器:就是把你不想要的或不想显示的内容给过滤掉。拦截器可以抽象出一部分代码可以用来完善原来的Action。同时可以减轻代码冗余,提高重用率。比如在登入一个页面时,如果要求用户密码、权限...
Struts2在com.opensymphony.xwork2.interceptor.annotations包中定义了3个拦截器注解类型。
(2)拦截器组件可以继承AbstractIntercepter类(实现了Intercepter接口)。 (3)拦截器组件可以继承MethodFilterIntercepter类(继承自AbstractIntercepter),增加了方法过滤功能(上面两种方式是拦截所有方法,这个...